數(shù)據(jù)庫--mysql

前記

最近一直在學(xué)習(xí)后臺,學(xué)習(xí)到了數(shù)據(jù)庫,記錄mysql中的常用語句,以便自己日后反查,如果還想更多的了解可以去官網(wǎng),這是非常詳細的中文官方文檔。

自己在mysql 客戶端實際操作了一遍,另外畫了腦圖,方便自己記憶和復(fù)習(xí)。

mysql語句總結(jié).png

1,使用MySQL,需要下載MySQL客戶端,具體操作自行百度即可。

記錄自己當(dāng)初犯下的錯誤,通過doc命令進入mysql :

  • mysql -u root -p(記住千萬不要輸';')

出現(xiàn)此界面說明成功登陸mysql 客戶端

以下是常用sql語句:

一,關(guān)于數(shù)據(jù)庫

1,創(chuàng)建數(shù)據(jù)庫

create database mydb ;

2,查看創(chuàng)建數(shù)據(jù)庫語句(查看mysql創(chuàng)建的源碼)

show create database mydb ;

3,使用數(shù)據(jù)庫(需要先調(diào)用此語句,才進行其他操作)

use mydb (可寫可不寫分號)

4,刪除數(shù)據(jù)庫

drop database mydb ;

5,查看所有的數(shù)據(jù)庫

show databases ;

6,修改數(shù)據(jù)庫mydb1的字符集為utf8

alter database mydb1 character set utf8 ;

7,了解

創(chuàng)建數(shù)據(jù)庫mydb1,字符集用gbk
create database mydb1 character set gbk ;
查看數(shù)據(jù)庫中所有的校對規(guī)則
show collation ;
查看中文的校驗規(guī)則
show collation like '%gb%' ;
創(chuàng)建數(shù)據(jù)庫mydb2,字符集用gbk,校驗規(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 ;

自動增長

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,'張無忌') ;
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 = '東方不敗' ;
修改多個字段
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增加一個字段address
alter table t4 add address varchar(100) ; 
刪除字段address
alter table t4 drop column address ;

7,查看表的結(jié)構(gòu)

desc t4 ;

三,DQL:數(shù)據(jù)查詢語言

創(chuàng)建一個學(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,'張無忌','男',20,'北京') ;
insert into stu values(2,'小龍女','女',18,'古墓') ;
insert into stu values(3,'黃蓉','女',15,'桃花島') ;
insert into stu values(4,'韋小寶','男',24,'揚州') ;
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ù)
  # _ 表示任意的一個字符
  select * from 表名 where 字段名 like 字段表達式
  #查詢所有以張開頭的人
  select * from stu where name like '張%' ;
  #查詢姓名中含有張這個字的人
  select * from stu where name like '%張%' ;
  #查詢姓名中含有張這個字的人并且姓名的長度是3個字的人
  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的子表),需要要添加此語句

constraint sid_fk foreign key(sid) references stu(id)
帶外鍵表的查詢源碼

2, 給字段起別名

select id as 編號,china as 語文,english as 英語,history as 歷史 from score ;
select id 編號,china 語文,english 英語,history 歷史 from score ;

3, 字段可以有表達式

  select id,china+10,english,history from score ;
  查看所有人考試的總分是多少
  select id,china + english + history 總分 from score ;
  查看總分大于200的人
  select * from score where china + english + history > 200 ;
別名和表達式一起使用1
別名和表達式一起使用2

3, or 和 in 字段查詢

 //查詢或
select * from stu where address = '桃花島' or address = '黒木崖' ;
 //查詢包含
select * from stu where address in('桃花島','黒木崖') ;

4, not int 字段查詢以及語句嵌套查詢

select id ,name from stu where id not in(select sid from score) ; 

5, null 字段查詢

select * from stu where address = null ; #錯誤的
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, 多個字段進行排序(語文升序,對語文成績一樣的人再進行歷史降序類排)

  select * from score order by china asc,history desc;(優(yōu)先前面的查詢)
  根據(jù)考試總分降序進行排序
  select *,china + english + history 總分 from score order by china + english + history desc ;

四,約束

1,外鍵約束

可以理解為一個特殊的字段

創(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. 添加記錄時必須先添加主表中的記錄,再添加字表中的記錄
   2. 不能更改主表中具有外鍵約束的記錄的主鍵
   3. 刪除記錄的時候不允許刪除具有外鍵關(guān)系的主表中的記錄
      (刪除的順序應(yīng)當(dāng)是先刪除字表中的記錄,然后刪除主表中的記錄)

六,多表查詢

1,交叉查詢

(cross  join ... on ...)查詢每個人的考試成績
select * from stu s cross join score c on s.id = c.sid ;  #交叉 比較了45次

(inner  join ... on ...)查詢參加考試的人的成績
select name,china,english,history,china+english+history 總分 
    from stu s inner join score c on  s.id = c.sid ;

(left join ...on...,以左表為主的查詢)查詢所有人的成績
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) ;

查詢參加考試的人的成績
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ù)多個字段進行分組
 //分組條件 group by ...having ...
 select count(*),sex from stu where age >=16 group by sex having count(*) >1 ;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容