MySQL:眾多關(guān)系型數(shù)據(jù)庫中的一種
倉庫 --數(shù)據(jù)庫
箱子 --表
數(shù)據(jù)庫:
進(jìn)入mysql 命令行: mysql -uroot -p
查看所有數(shù)據(jù)庫: show databases;
創(chuàng)建數(shù)據(jù)庫: create database niu charset utf8;
刪除數(shù)據(jù)庫: drop database niu;
選擇數(shù)據(jù)庫: use databases;
查看所有表: show tables;
查看創(chuàng)建數(shù)據(jù)庫的語句:show create database databasename;
查看創(chuàng)建表的語句:show create table tablename;
查看表結(jié)構(gòu):desc tablenmae;
表:
約束
#自增長
auto_increment
#非空
not null
#默認(rèn)值
default 'xx'
#唯一
unique
#指定字符集
charset
#主鍵
primary key
#外鍵
增加兩個表之間的聯(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,
);
#成績表
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 ;#查看新建表語句
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ù),自增長id從頭再來,快速阶剑,從磁盤直接刪除吃沪,不可恢復(fù)
delete from student汤善;?
#刪除整個表的數(shù)據(jù),自增長繼續(xù)
改
update student set money=100票彪;#不指定條件红淡,修改所有
update student set money=110 where name='hk';#只改hk
自動提交
取消自動提交 ? set @@autocommitt=0;
? ? ? ? ? ? ? ? select @@autocommitt=0;
#自動提交取消后,當(dāng)前會話顯示已經(jīng)成功執(zhí)行降铸,其實后臺并沒有執(zhí)行
查:
select * from students limit 1,5; #從第幾條開始在旱,下面的x條,不包含開始的那一條
SELECT * from students limit 5;查詢5條
SELECT id,stu_name,sex,money,phone from students;#指定查詢的字段
SELECT * from students;#查詢所有的數(shù)據(jù)
SELECT * from students where sex='男';#指定條件
SELECT * from students where sex='男' and money>100; #多個條件推掸,必須同時滿足
SELECT * from students where sex='男' or sex='未知' ; #多個條件桶蝎,有一個滿足即可
SELECT * from students where sex !='男'; #<>也是不等于
SELECT * FROM students where addr like '%東京%';#模糊匹配,%代表的是通配符谅畅,必須得用like
SELECT * from students a where a.stu_name like '姚_';#_通配符表示任意一個單字符登渣,姚字后面只能跟一個字
SELECT a.stu_name '學(xué)生名稱',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ù)哪個字段繼續(xù)排序铃彰,默認(rèn)是升序绍豁,
降序是desc,升序asc
SELECT * from students a where a.addr = '' or a.addr is null; #查詢字段為空的數(shù)據(jù)
SELECT DISTINCT a.money from students a ;#去重
SELECT COUNT(*) '學(xué)生人數(shù)' from students where sex='女'; #統(tǒng)計行數(shù)
SELECT MAX(a.money) 錢最多 from students a; #最大值
SELECT min(money) 錢最少 from students;#最小值
SELECT AVG(a.money) 平均多少錢 from students a; #平均數(shù)
SELECT sum(a.money) 總共多少錢 from students a;#總和
SELECT sex 性別,count(*) 人數(shù) from students GROUP BY sex; #分組
SELECT
sex 性別,
count(*) 人數(shù),
a.stu_name 名字
FROM
students aWHERE?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必須寫在group by后面
SELECT *,COUNT(*) from students GROUP BY sex,class; #多個字段進(jìn)行分組
SELECT id,stu_name from students UNION SELECT id,t_name from teacher;
#用來合并兩條select語句的結(jié)果芬位,兩條select語句字段數(shù)量要一致,并且數(shù)據(jù)類型也要一致
union和union all的區(qū)別就是一個會去重一個不會
多表關(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)
-- 兩個表里面都存在的數(shù)據(jù)查出來
SELECT * from students a LEFT JOIN scores b on a.id=b.s_id;
-- LEFT JOIN會把左邊表所有的數(shù)據(jù)都查出來带到,右邊表有匹配的就查出來
SELECT * from students a RIGHT JOIN scores b on a.id=b.s_id;
-- RIGHT JOIN會把右邊表所有的數(shù)據(jù)都查出來昧碉,左邊表有匹配的就查出來
SELECT * from students a inner JOIN scores b on a.id=b.s_id;
-- INNER JOIN兩邊表里都匹配的數(shù)據(jù)才查到
子查詢:
把一條sql的結(jié)果,作為另一條sql的條件
SELECT * from scores a where a.s_id = (SELECT id from students where stu_name='牛牛');
把子查詢當(dāng)成一個表
SELECT
a.grade 成績,
b.stu_name 學(xué)生名稱,
b.id 學(xué)號
FROM
scores a,
(SELECT?id,stu_name?FROM?students?WHERE?stu_name = '牛牛') b
WHERE
a.s_id = b.id;
數(shù)據(jù)庫權(quán)限:
mysql數(shù)據(jù)的權(quán)限實質(zhì)上都是在user表里控制的
1揽惹、grant
#所有的權(quán)限 所有數(shù)據(jù)庫下面的所有表 用戶 用戶ip?
grant all on *.* to 'andashu'@'localhost' IDENTIFIED BY '123456' with grant option;
密碼 #有執(zhí)行g(shù)rant語句的權(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ù)
對user表進(jìn)行增加、修改和刪除
flush privileges;#刷新權(quán)限
備份數(shù)據(jù)庫:
mysqldump -uroot -p123456 db > db.sql
mysqldump -uroot -p123456 -A > all.sql
恢復(fù)數(shù)據(jù):
mysql -uroot -p123456 db < db.sql
存儲過程:
批量的造數(shù)據(jù)
delimiter $$; #為了改結(jié)束符
CREATE PROCEDURE big_data1(num int)#代表要造多少條數(shù)據(jù) 100
BEGIN
DECLARE i int;
set i=0;
WHILE i
insert into students (stu_name,money) VALUES (CONCAT('小明',i),20000);
#CONCAT的作用是連接不同類型的數(shù)據(jù)
#把字符串和數(shù)字拼接到一起
set i=i+1;
end WHILE;
End
$$;
delimiter;
call big_data1(500); #調(diào)用