MySQL:眾多關(guān)系型數(shù)據(jù)庫(kù)中的一種
倉(cāng)庫(kù) --數(shù)據(jù)庫(kù)
箱子 --表
數(shù)據(jù)庫(kù):
進(jìn)入mysql 命令行: mysql -uroot -p
查看所有數(shù)據(jù)庫(kù): show databases;
創(chuàng)建數(shù)據(jù)庫(kù): create database niu charset utf8;
刪除數(shù)據(jù)庫(kù): drop database niu;
選擇數(shù)據(jù)庫(kù): use databases;
查看所有表: show tables;
查看創(chuàng)建數(shù)據(jù)庫(kù)的語(yǔ)句:show create database databasename;
查看創(chuàng)建表的語(yǔ)句:show create table tablename;
查看表結(jié)構(gòu):desc tablenmae;
表:
約束
#自增長(zhǎng)
auto_increment
#非空
not null
#默認(rèn)值
default 'xx'
#唯一
unique
#指定字符集
charset
#主鍵
primary key
#外鍵
增加兩個(gè)表之間的聯(lián)系
增:
#學(xué)生表
create table students(
id int auto_increment primary key,
name varchar(10) not null,
sex varchar(3) default '女',
address varchar(50),
phone int not null unique,
age,
);
#成績(jī)表
create table scores(
id int auto_increnent primary key,
s_id int not null,
grade float not null,
);
刪:
drop table tablename;
truncate tablename;#快速刪除表
改:
alter table oldtable rename newtable; #改表名
alter table tablename modify name varchar(20);#改表結(jié)構(gòu)
alter table tablename change name newname varchar(20);#改表結(jié)構(gòu)
alter (table tablename add age float
after name;#新增字段的位置
查:
show create table tablename ;#查看新建表語(yǔ)句
desc table;#查看表結(jié)構(gòu)
show tables ;#查看所有表
數(shù)據(jù):
增
insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
insert into student values('','小明',100,'',120);
刪
turncate tablename; #刪除整表數(shù)據(jù),自增長(zhǎng)id從頭再來(lái),快速,從磁盤(pán)直接刪除,不可恢復(fù)
delete from student;
#刪除整個(gè)表的數(shù)據(jù),自增長(zhǎng)繼續(xù)
改
update student set money=100;#不指定條件,修改所有
update student set money=110 where name='hk';#只改hk
自動(dòng)提交
取消自動(dòng)提交 set @@autocommitt=0;
select @@autocommitt=0;
#自動(dòng)提交取消后,當(dāng)前會(huì)話顯示已經(jīng)成功執(zhí)行,其實(shí)后臺(tái)并沒(méi)有執(zhí)行
查:
select * from students limit 1,5; #從第幾條開(kāi)始,下面的x條,不包含開(kāi)始的那一條
SELECT * from students limit 5;查詢(xún)5條
SELECT id,stu_name,sex,money,phone from students;#指定查詢(xún)的字段
SELECT * from students;#查詢(xún)所有的數(shù)據(jù)
SELECT * from students where sex='男';#指定條件
SELECT * from students where sex='男' and money>100; #多個(gè)條件,必須同時(shí)滿(mǎn)足
SELECT * from students where sex='男' or sex='未知' ; #多個(gè)條件,有一個(gè)滿(mǎn)足即可
SELECT * from students where sex !='男'; #<>也是不等于
SELECT * FROM students where addr like '%東京%';#模糊匹配,%代表的是通配符,必須得用like
SELECT * from students a where a.stu_name like '姚_';#_通配符表示任意一個(gè)單字符,姚字后面只能跟一個(gè)字
SELECT a.stu_name '學(xué)生名稱(chēng)',a.phone '學(xué)生電話' from students as a where a.stu_name='姚遠(yuǎn)';#給表起別名,as可以省略
SELECT * from students a where a.stu_name in ('牛牛','林倩','姚遠(yuǎn)');# in
SELECT * from students a where a.money BETWEEN 1000 and 10000;#在什么什么之間的數(shù)據(jù)
SELECT * from students ORDER BY money desc;
#order by xxx desc,根據(jù)哪個(gè)字段繼續(xù)排序,默認(rèn)是升序,
降序是desc,升序asc
SELECT * from students a where a.addr = '' or a.addr is null; #查詢(xún)字段為空的數(shù)據(jù)
SELECT DISTINCT a.money from students a ;#去重
SELECT COUNT(*) '學(xué)生人數(shù)' from students where sex='女'; #統(tǒng)計(jì)行數(shù)
SELECT MAX(a.money) 錢(qián)最多 from students a; #最大值
SELECT min(money) 錢(qián)最少 from students;#最小值
SELECT AVG(a.money) 平均多少錢(qián) from students a; #平均數(shù)
SELECT sum(a.money) 總共多少錢(qián) from students a;#總和
SELECT sex 性別,count(*) 人數(shù) from students GROUP BY sex; #分組
SELECT
sex 性別,
count(*) 人數(shù),
a.stu_name 名字
FROM
students a WHERE a.money > 300 GROUP BY a.id HAVING a.stu_name LIKE '姚%';
#如果group by后面有條件的話,必須得用having子句,having子句里面用到的字段必須出現(xiàn)在select后面,如果group by和order by一起用的話,order by必須寫(xiě)在group by后面
SELECT *,COUNT(*) from students GROUP BY sex,class; #多個(gè)字段進(jìn)行分組
SELECT id,stu_name from students UNION SELECT id,t_name from teacher;
#用來(lái)合并兩條select語(yǔ)句的結(jié)果,兩條select語(yǔ)句字段數(shù)量要一致,并且數(shù)據(jù)類(lèi)型也要一致
union和union all的區(qū)別就是一個(gè)會(huì)去重一個(gè)不會(huì)
多表關(guān)聯(lián):
SELECT * FROM USER a, accounts b WHERE
a.id = b.user_id
AND a.username = 'niuhy';
-- SELECT * from students a ,scores b where a.id=b.s_id; -- 多表關(guān)聯(lián)
-- 兩個(gè)表里面都存在的數(shù)據(jù)查出來(lái)
SELECT * from students a LEFT JOIN scores b on a.id=b.s_id;
-- LEFT JOIN會(huì)把左邊表所有的數(shù)據(jù)都查出來(lái),右邊表有匹配的就查出來(lái)
SELECT * from students a RIGHT JOIN scores b on a.id=b.s_id;
-- RIGHT JOIN會(huì)把右邊表所有的數(shù)據(jù)都查出來(lái),左邊表有匹配的就查出來(lái)
SELECT * from students a inner JOIN scores b on a.id=b.s_id;
-- INNER JOIN兩邊表里都匹配的數(shù)據(jù)才查到
子查詢(xún):
把一條sql的結(jié)果,作為另一條sql的條件
SELECT * from scores a where a.s_id = (SELECT id from students where stu_name='牛牛');
把子查詢(xún)當(dāng)成一個(gè)表
SELECT
a.grade 成績(jī),
b.stu_name 學(xué)生名稱(chēng),
b.id 學(xué)號(hào)
FROM
scores a,
( SELECT id,stu_name FROM students WHERE stu_name = '牛牛') b
WHERE
a.s_id = b.id;
數(shù)據(jù)庫(kù)權(quán)限:
mysql數(shù)據(jù)的權(quán)限實(shí)質(zhì)上都是在user表里控制的
1、grant
#所有的權(quán)限 所有數(shù)據(jù)庫(kù)下面的所有表 用戶(hù) 用戶(hù)ip
grant all on *.* to 'andashu'@'localhost' IDENTIFIED BY '123456' with grant option;
密碼 #有執(zhí)行g(shù)rant語(yǔ)句的權(quán)限
grant all on *.* to 'andashu'@'%' IDENTIFIED BY '123456' with grant option;
取消授權(quán):
Revoke select on *.* from dba@localhost;
Revoke all on *.* from andashu@localhost;
2、修改user表的數(shù)據(jù)
對(duì)user表進(jìn)行增加、修改和刪除
flush privileges;#刷新權(quán)限
備份數(shù)據(jù)庫(kù):
mysqldump -uroot -p123456 db > db.sql
mysqldump -uroot -p123456 -A > all.sql
恢復(fù)數(shù)據(jù):
mysql -uroot -p123456 db < db.sql
存儲(chǔ)過(guò)程:
批量的造數(shù)據(jù)
delimiter $$; #為了改結(jié)束符
CREATE PROCEDURE big_data1(num int)#代表要造多少條數(shù)據(jù) 100
BEGIN
DECLARE i int;
set i=0;
WHILE i<num do
insert into students (stu_name,money) VALUES (CONCAT('宋灝志',i),20000);
#CONCAT的作用是連接不同類(lèi)型的數(shù)據(jù)
#把字符串和數(shù)字拼接到一起
set i=i+1;
end WHILE;
End
$$;
delimiter;
call big_data1(500); #調(diào)用
聯(lián)系客服