MySQL常用代碼

來源:互聯網
上載者:User

標籤:

create database 資料庫名

create table CeShi1
(
Uid varchar(50) primary key,
Pwd varchar(50),
Name varchar(50),
Nation varchar(50),
foreign key(Nation) references Nation(Code)
)

寫查詢語句需要注意:
1.建立表的時候,最後一列後面不要寫逗號
2.如果有多條語句一起執行,注意在語句之間加分號分隔
3.寫代碼所有符號都是半形的

關係型資料庫:表和表之間是有關係存在的


建立表的幾個關鍵字:
1.主鍵:primary key
2.非空:not null
3.自增長列:auto_increment
4.外鍵關係:foreign key(列名) references 表名(列名)


CRUD操作:   增刪改查

1.添加資料:
insert into Info values(‘‘,‘‘,‘‘,‘‘,‘‘) 要求values括弧裡面的值的個數要和表裡面列數相同
insert into Info (Code,Name) values(‘‘,‘‘) 添加指定列的值

2.修改資料
update Info set Name = ‘張三‘ where Code = ‘p001‘

3.刪除資料
delete from Info where Code = ‘p001‘

alter table 表名     可在已有的表中增加,刪除和修改列

主鍵約束 primary key #一個表中只能有一個主鍵

alter table 表名 add primary key(列名)

唯一約束 unique #一個表中可以有多個unique

alter table 表名 add unique(列名)

外鍵約束 foreign key ···references #一個表中可以有多個外鍵

altre table 外鍵表名 add foreign key (列名) references 主鍵表名(列名)


查詢資料:

1.普通查詢,查所有的
select * from Info #查所有資料
select Code,Name from Info #查指定列

2.條件查詢
select * from Info where Code = ‘p001‘ #一個條件
select * from Info where Name = ‘張三‘ and Nation = ‘n001‘ #兩個條件並的關係
select * from Info where Name = ‘張三‘ or Nation = ‘n001‘ #兩個條件或的關係

3.排序查詢
select * from Info order by Birthday #預設升序排列asc 如果要降序排列 desc
select * from Car order by Brand,Oil desc #多列排序

4.彙總函式
select count(*) from Info #取個數
select sum(Price) from Car #查詢price列的和
select avg(Price) from Car #查詢price列的平均值
select max(Price) from Car #查詢price列的最大值
select min(Price) from Car #查詢price列的最小值

5.分頁查詢
select * from Car limit n,m #跳過n條資料取m條資料
select * from Car limit (n-1)*m,m #第二頁跳過(n-1)*m條資料取m條資料

6.分組查詢
select Brand from Car group by Brand #簡單分組查詢
select Brand from Car group by Brand having count(*)>2 #查詢系列裡面車的數量大於2的系列

7.去重查詢
select distinct Brand from Car

8.修改列名
select Brand as ‘系列‘ from Car

9.模糊查詢
select * from Car where Name like ‘_迪%‘       %代表任意多個字元 _代表一個字元

10.離散查詢
select * from Car where Code in (‘c001‘,‘c002‘,‘c003‘,‘c004‘)
select * from Car where Code not in (‘c001‘,‘c002‘,‘c003‘,‘c004‘)

進階查詢:

1.串連查詢

select * from Info,Nation #得出的結果稱為笛卡爾積
select * from Info,Nation where Info.Nation = Nation.Code

join on串連

select * from Info join Nation #join串連
select * from Info join Nation on Info.Nation = Nation.Code


2.聯集查詢

select Code,Name from Info
union
select Code,Name from Nation


3.子查詢

1)無關子查詢

select Code from Nation where Name = ‘漢族‘ #去Nation表中查詢漢族的民族代號

select * from Info where Nation = (民族代號) #在Info表中查詢民族代號為上一個查詢結果的所有資訊

select * from Info where Nation = (select Code from Nation where Name = ‘漢族‘)

子查詢查詢的結果被父查詢使用,子查詢可以單獨執行的稱為無關子查詢

 

2)相互關聯的子查詢

select * from Car where Oil<(該系列的平均油耗) #查詢油耗小於該系列平均油耗的

select avg(Oil) from Car where Brand = "值" #查詢某系列的平均油耗

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)    #a, b為別名

#內層查詢時外層查詢是固定的: 查詢時按順序一條一條查詢

select avg(Oil) from Car b where b.Brond = ‘b001‘ #結果為8.7

#外層查詢就變為:

select * from Car where Oil < 8.7

 

MySQL常用代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.