SQL全稱Structured Query Language 結(jié)構(gòu)化查詢語言庵佣,跟編程語言有明顯的語言區(qū)別歉胶,編程語言例如PHP、JAVA巴粪、C等是HOW型怎么去設(shè)計(jì)的語言通今,而SQL為WHAT型查詢是什么的語言粥谬。
在SQL的執(zhí)行上可以分為三個種類,且各種類的執(zhí)行角度辫塌,實(shí)際我們編程操作中的比例各不相同漏策。
數(shù)據(jù)操作語言:DML is Data Manipulation Language statements,SQL中處理數(shù)據(jù)等操作統(tǒng)稱為數(shù)據(jù)操縱語言臼氨,為數(shù)據(jù)庫使用角度掺喻,所占比例大概為80%,即大部分SQL操作中都是DML储矩,而在DML中80%操作為增刪改查中的查感耙,對SELECT查中的靈活運(yùn)用是SQL程序猿需要掌握的內(nèi)容。
數(shù)據(jù)定義語言:DDL is Data Definition Language statements持隧,SQL中用于定義和管理數(shù)據(jù)庫中的所有對象的語言即硼,為數(shù)據(jù)庫建設(shè)角度,例如:建表屡拨、建庫只酥、建視圖等,所占比例大概為15%呀狼。
數(shù)據(jù)控制語言:DCL is Data Control Language statements裂允,SQL中用于授權(quán)或回收訪問數(shù)據(jù)庫的某種特權(quán),為數(shù)據(jù)庫建設(shè)角度哥艇,實(shí)現(xiàn)控制數(shù)據(jù)庫操縱事物發(fā)生的時間及效果绝编,對數(shù)據(jù)庫實(shí)行監(jiān)視等,所占比例5%她奥。
請點(diǎn)擊此處輸入圖片描述
下面是附上MySql的基本語法
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í)行厚掷,其實(shí)后臺并沒有執(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)計(jì)行數(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 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必須寫在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)限實(shí)質(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)用