一、Sql語言概述mysql
a) ddl語句(數據定義語言):create,alter,dropsql
b) dml語句(數據操做語言):update,insert,delete,select數據庫
c) dcl語句(數據控制語言):是數據庫控制功能。用來設置或更改數據庫用戶或角色權限的語句,包括(grant,deny,revoke等)語句服務器
二、鏈接和登陸數據庫ide
a) Mysql -h host -P3306 -uuser -ppassword函數
b) -h:當鏈接MySQL服務器不在同臺主機時,填寫主機名或IP地址,默認是localhostspa
c) -P:訪問數據庫的端口,默認是3306設計
d) -u:登陸MySQL的用戶名3d
e) -p:登陸MySQL的密碼對象
三、存儲引擎
a) Show engines:能夠查看MySQL服務實例支持的存儲引擎
b) InnoDB(行級鎖):支持外鍵(foreign key),支持事務(transaction),在修改數據的時候只修改一條數據,在操做其餘數據的時候還能夠正常操做
c) MyISAM(表鎖):不支持事務、外鍵,指定表的存儲引擎eg:create table
四、常見的惟一性約束
a) Primary key:主鍵約束
b) Unique:惟一性約束
c) Not null:非空值約束
d) Auto_increment:用於整列默認遞增
e) Default default_value:默認值約束
五、查看錶結構
a) Desc 表名:查看錶結構
b) Show create table 表名:查看建表的語句
六、DDL語句
a) Alter table 表名:修改表名 eg:alter table hyn_test rename hyn
b) 修改字段名
1.alter table hyn modify age varchar(5); #把hyn表中的age字段的數據類型改爲varchar類型
2.alter table hyn change age ag varchar(5); #同時修改類型和字段名字
3.選中某個表,右鍵設計表也能夠修改表中的字段
c) 增長字段:alter table 表名add 屬性名 數據類型[完整性約束條件] [first | after 屬性名2]
d) 刪除字段:alter table 表名 drop age;
e) 刪除表
1.truncate table 表名; #刪除以後沒法恢復,自增加id從1開始
2.delete from 表名; #刪除以後能夠回滾,自增加id從上一個id以後開始增長
3.drop 表名; #刪除以後沒法回滾
f) 增長外鍵:alter table 表名 add constraint FK_ID foreign key(外鍵字段名) references 外表表名(對應的表的主鍵字段名);
g) 刪除標的外鍵約束:alter table 表名 drop foreign key 外鍵別名;
七、DML語句
a) 添加數據
1.insert into 表名(要插入的表的字段) values(); #指定字段
2.insert into 表名 values(); #必需要填寫完全部的列名
3.set autocommit=0; #關閉自動提交
4.commit #開啓自動提交
b) 修改表數據
1.update 表名 set stu_name='張山' where stu_id=1; #修改id爲1的用戶名
2.update 表名 set stu_name='張山',cn='JP' where stu_id=1; #修改多個字段
3.update 表名 set stu_name='張山',cn='JP' limit 5,10; #修改第5到10行的數據,(基本上沒有多大意義,只須要了解怎麼用便可)
c) 查詢語句
1.select * from 表名 where age is null or age=''; #查詢空數據
2.select * from 表名 where stu_name like '張%'; #查詢以張開頭的用戶
3.select * from 表名 where stu_name like '張_'; #匹配任意一個字符
d) 使用聚合函數查詢
1.avg,sum,max,min,count; eg:select avg(sarlary) from 表名; #單獨使用聚合函數
2.group by; eg:select count(*),sex from 表名 group by sex; #聚合函數和分組函數一塊兒使用
3.分組以後有條件的話不能直接加where,用having,eg:select stu_id from score group by stu_id having count(*) < 2
e) 備份表
1.create table 表名 like 表名(數據庫中已有的表); #建立和數據庫中已存在的表相同表結構的表
2.create table 表名_bak as select * from 表名; #先建立一個表,再把這個表的數據所有加入到新建的表中
f) 查詢和修改
1.left join:查詢出左表的全部成績,右表中沒有成績的數據不會查詢出來
2.right join:查詢出右表的全部成績,左表中沒有成績的不會查詢出來
3.inner join:查詢連個表的全部數據
4.update 表1,表2 set 字段1=,字段2= where 表1.id=表2.id and... #多表修改
g) 去重
1.select distinct 字段名 from 表名; #去掉重複的字段名
2.union,有重複的直接去重,條件比較多,不知道怎麼關聯的時候用, eg:select 字段1,字段2 from 表名 where 條件 union select 字段1,字段2 from 表名 條件;
3.union all,有重複的數據不會去重
h) 子查詢
1.select * from 表名 where id=(select id from 表名 where 條件);
2.update 表名 set 字段名=‘’ where id=(select ........);
3.把子查詢的結果當成一個表來操做,以下圖
八、DCL語句
a) Grant all on *.* to ‘root’@’localhost’identified by ‘123456’with grant option; #增長一個超級用戶
b) Grant select,insert,update on bugfree.* to ‘test’@’%’ identified by ‘123456’ #增長一個普通用戶
c) Revoke 權限 on 數據庫對象 from 用戶 #取消權限
d) 即容許本機鏈接又運行遠程鏈接須要加localhost和%
九、數據庫備份
a) Mysqldump,eg:mysqldump -uroot -p123456 數據庫名 [表名]> 數據庫名.sql;
b) 備份整個數據庫,eg:mysqldump -uroot -p123456 -A > all.sql
c) 恢復數據庫備份,eg:mysql -uroot -p123456 數據庫名 < xxx.sql
十、寫一個存儲過程,實現向某個表中自動添加500條數據
#默認狀況下,MySQL一遇到「;」就會自動執行,在這樣的狀況下就須要事先把delimiter後面的符號換成「$$」或「//」;加$$的目的就是爲了讓整個語句寫完以後在執行
delimiter $$;
create procedure test_student(count int)
begin
declare name varchar(20); #聲明變量
declare sex varchar(10);
declare addr varchar(50);
declare class varchar(20);
declare i int;
set i = 0; #設置變量的值
set sex= 'F';
set addr='beijingchaoyang';
set class='wusuopu';
while i<count do #開始循環
set name=CONCAT('difeng',i); #鏈接字符串函數
insert into students (name,sex,class,addr) values (name,sex,class,addr); #插入數據操做
set i=i+1;
end while; #結束循環
end
$$;
delimiter;
call test_student(500); #調用存儲過程
select count(*) from students; #查詢students表的總數據