前記
最近一直在學(xué)習(xí)后臺(tái),學(xué)習(xí)到了數(shù)據(jù)庫(kù)蹭睡,記錄mysql中的常用語(yǔ)句衍菱,以便自己日后反查,如果還想更多的了解可以去官網(wǎng)肩豁,這是非常詳細(xì)的中文官方文檔脊串。
自己在mysql 客戶端實(shí)際操作了一遍辫呻,另外畫了腦圖,方便自己記憶和復(fù)習(xí)琼锋。
1放闺,使用MySQL,需要下載MySQL客戶端缕坎,具體操作自行百度即可怖侦。
記錄自己當(dāng)初犯下的錯(cuò)誤,通過doc命令進(jìn)入mysql :
- mysql -u root -p(記住千萬(wàn)不要輸';')
出現(xiàn)此界面說明成功登陸mysql 客戶端
以下是常用sql語(yǔ)句:
一谜叹,關(guān)于數(shù)據(jù)庫(kù)
1匾寝,創(chuàng)建數(shù)據(jù)庫(kù)
create database mydb ;
2,查看創(chuàng)建數(shù)據(jù)庫(kù)語(yǔ)句(查看mysql創(chuàng)建的源碼)
show create database mydb ;
3荷腊,使用數(shù)據(jù)庫(kù)(需要先調(diào)用此語(yǔ)句艳悔,才進(jìn)行其他操作)
use mydb (可寫可不寫分號(hào))
4,刪除數(shù)據(jù)庫(kù)
drop database mydb ;
5女仰,查看所有的數(shù)據(jù)庫(kù)
show databases ;
6猜年,修改數(shù)據(jù)庫(kù)mydb1的字符集為utf8
alter database mydb1 character set utf8 ;
7,了解
創(chuàng)建數(shù)據(jù)庫(kù)mydb1,字符集用gbk
create database mydb1 character set gbk ;
查看數(shù)據(jù)庫(kù)中所有的校對(duì)規(guī)則
show collation ;
查看中文的校驗(yàn)規(guī)則
show collation like '%gb%' ;
創(chuàng)建數(shù)據(jù)庫(kù)mydb2,字符集用gbk,校驗(yàn)規(guī)則用gbk_bin
create database mydb2 character set gbk collate gbk_bin ;
設(shè)置客戶端的字符集為gbk
set character_set_client=gbk;
設(shè)置結(jié)果集的字符集為gbk
set character_set_results=gbk ;
二,關(guān)于表
1,創(chuàng)建表t
create table t(
id int ,
name varchar(30)
) ;
創(chuàng)建表t1,使用字符集gbk
create table t1(
id int ,
name varchar(30)
)character set gbk ;
自動(dòng)增長(zhǎng)
create table t2
(
id int primary key auto_increment,
name varchar(20)
) ;
2,查看創(chuàng)建表的源碼
show create table t ;
3,插入數(shù)據(jù)
insert into t(id,name) values(1,'張無(wú)忌') ;
insert t(id,name) values(2,'喬峰') ;
省略字段董栽,意味著所有的字段都必須給值(自增例外)
insert t4 values(3,'楊過','2014-4-3') ;
4,更新
將表t4的第三條記錄姓名字段改為楊康
update t4 set name='楊康' where id = 3 ;
將所有記錄的名字都改為東方不敗
update t4 set name = '東方不敗' ;
修改多個(gè)字段
update t4 set id=6,name='蕭峰' where id = 2 ;
5,刪除
刪除表格
drop table t4;
刪除所有的記錄
delete from t4 where id = 4 ;
delete from t4 ;
truncate table t4 ;
6,字段處理
給表t4增加一個(gè)字段address
alter table t4 add address varchar(100) ;
刪除字段address
alter table t4 drop column address ;
7码倦,查看表的結(jié)構(gòu)
desc t4 ;
三,DQL:數(shù)據(jù)查詢語(yǔ)言
創(chuàng)建一個(gè)學(xué)生表
create table stu
(
id int primary key, #主鍵約束
name varchar(30) unique, #唯一約束
sex char(2) not null, #非空約束
age int ,
address varchar(50) default '北京' #默認(rèn)約束
) ;
insert into stu values(1,'張無(wú)忌','男',20,'北京') ;
insert into stu values(2,'小龍女','女',18,'古墓') ;
insert into stu values(3,'黃蓉','女',15,'桃花島') ;
insert into stu values(4,'韋小寶','男',24,'揚(yáng)州') ;
insert into stu values(5,'喬峰','男',34,'雁門關(guān)') ;
insert into stu values(6,'張果老','男',30,'雁門關(guān)') ;
insert into stu values(7,'老張','男',38,'黒木崖') ;
insert into stu values(8,'張','男',34,'桃花島') ;
insert into stu values(9,'韋小寶','女',24,'新東方') ;
insert into stu(id,name,sex,age) values(10,'令狐沖','男',27) ;
1,查看所有數(shù)據(jù)
select * from stu ;
2锭碳,查看小龍女的信息
select * from stu where id = 2 ;
select * from stu where name='小龍女' ;
3袁稽,查看年齡在20~30之間的人
select * from stu where age >=20 and age <=30 ;
select * from stu where age between 20 and 30 ; # 包括20和30
4,查看所有的的姓名
select name from stu ;
查看所有的的姓名,年齡擒抛,性別
select name,age,sex from stu ;
5推汽,模糊查詢
# % 表示任意字符數(shù)
# _ 表示任意的一個(gè)字符
select * from 表名 where 字段名 like 字段表達(dá)式
#查詢所有以張開頭的人
select * from stu where name like '張%' ;
#查詢姓名中含有張這個(gè)字的人
select * from stu where name like '%張%' ;
#查詢姓名中含有張這個(gè)字的人并且姓名的長(zhǎng)度是3個(gè)字的人
select * from stu where name like '張__' or name like '_張_' or name like '__張' ;
6,distinct
查詢表中有幾種性別
select distinct sex from stu ;
查找姓名和性別整體都不同的記錄
select distinct name,sex from stu ;
創(chuàng)建分?jǐn)?shù)表
create table score
(
id int primary key,
sid int ,
china int,
english int ,
history int,
constraint sid_fk foreign key(sid) references stu(id)
) ;
insert into score values(1,1,68,54,81) ;
insert into score values(2,3,89,98,90) ;
insert into score values(3,4,25,60,38) ;
insert into score values(4,6,70,75,59) ;
insert into score values(5,8,60,65,80) ;
1, 建帶有外鍵的表(即score是stu的子表)歧沪,需要要添加此語(yǔ)句
constraint sid_fk foreign key(sid) references stu(id)
2, 給字段起別名
select id as 編號(hào),china as 語(yǔ)文,english as 英語(yǔ),history as 歷史 from score ;
select id 編號(hào),china 語(yǔ)文,english 英語(yǔ),history 歷史 from score ;
3, 字段可以有表達(dá)式
select id,china+10,english,history from score ;
查看所有人考試的總分是多少
select id,china + english + history 總分 from score ;
查看總分大于200的人
select * from score where china + english + history > 200 ;
3, or 和 in 字段查詢
//查詢或
select * from stu where address = '桃花島' or address = '黒木崖' ;
//查詢包含
select * from stu where address in('桃花島','黒木崖') ;
4, not int 字段查詢以及語(yǔ)句嵌套查詢
select id ,name from stu where id not in(select sid from score) ;
5, null 字段查詢
select * from stu where address = null ; #錯(cuò)誤的
select * from stu where address is null ;
#查詢有地址的人
select * from stu where address is not null ;
五 排序(order by )
1, 升序排列
select * from score order by china asc;
2, 降序排列
select * from score order by history desc;
3, 多個(gè)字段進(jìn)行排序(語(yǔ)文升序歹撒,對(duì)語(yǔ)文成績(jī)一樣的人再進(jìn)行歷史降序類排)
select * from score order by china asc,history desc;(優(yōu)先前面的查詢)
根據(jù)考試總分降序進(jìn)行排序
select *,china + english + history 總分 from score order by china + english + history desc ;
四,約束
1诊胞,外鍵約束
可以理解為一個(gè)特殊的字段
創(chuàng)建引用約束
alter table score add constraint stu_score_fk foreign key(sid) references stu(id) ;
刪除約束
alter table score drop foreign key stu_score_fk ;
2暖夭,引用約束
注意:
1. 添加記錄時(shí)必須先添加主表中的記錄,再添加字表中的記錄
2. 不能更改主表中具有外鍵約束的記錄的主鍵
3. 刪除記錄的時(shí)候不允許刪除具有外鍵關(guān)系的主表中的記錄
(刪除的順序應(yīng)當(dāng)是先刪除字表中的記錄撵孤,然后刪除主表中的記錄)
六迈着,多表查詢
1,交叉查詢
(cross join ... on ...)查詢每個(gè)人的考試成績(jī)
select * from stu s cross join score c on s.id = c.sid ; #交叉 比較了45次
(inner join ... on ...)查詢參加考試的人的成績(jī)
select name,china,english,history,china+english+history 總分
from stu s inner join score c on s.id = c.sid ;
(left join ...on...,以左表為主的查詢)查詢所有人的成績(jī)
select name,china,english,history,china+english+history 總分
from stu s left out join score c on s.id = c.sid ;
(not int)查詢沒有參加考試的人
select * from stu where id not in(select sid from score) ;
查詢參加考試的人的成績(jī)
select name,china,english,history,china+english+history 總分
from stu s,score c where s.id = c.sid ;
2邪码,聚合函數(shù)
-
sum max,min avg ,count
//stu表中最大年齡最大的 select max(age) from stu; //score表中china的平均值 select avg(china) from score;
3裕菠,分組函數(shù)(group by)
select count(*) 數(shù)量,sex,name from stu group by sex,name ; #根據(jù)多個(gè)字段進(jìn)行分組
//分組條件 group by ...having ...
select count(*),sex from stu where age >=16 group by sex having count(*) >1 ;