資料庫——基礎(資料庫操作,表格操作)——增加高階查詢

weixin_33860553發表於2016-01-15

 

 

 

筆記

LAMP:Linx(作業系統)
A(阿帕奇)——網頁的應用程式
M(Mysql):體積小,應用簡單
P(PHP)

 

第一步:搭建網頁環境——A\M\P

WAMP:用WAMP搭建環境

DW:更好的顯示

資料庫的基本操作:

資料庫——表結構——欄位(列)

每一行資料成為一條資料(記錄)

特點:關係型資料庫,有嚴格的規範

1、必須有主鍵:能夠唯一標識一條資料的欄位

2

T-SQL:通用的資料庫操作語句

自增長列code(主鍵列) ;連線鍵表 最後一個欄位不加 ,#註釋

建立表:
create table Family
(
Code varchar(50) primary key,
Name varchar(50) not null,
Sex bit

);
create table Nation
(
Code varchar(50) primary key,
Name varchar(50)
);

create table Info
(
Code int auto_increment primary key,
Name varchar(50),
Sex bit,
Birthday date,
Height float,
Nation varchar(50) references Nation(Code) //一般不寫
)
primary key:主鍵
not null:非空
auto_increment:自增長列,整形,自動增長不用新增
references:引用 外來鍵關係

刪除表
drop table Family

建立資料庫:create database mydb

在資料庫中增加一列:alter table t1 add column addr varchar(20) not null;
這條語句會向已有的表t1中加入一列addr,這一列在表的最後一列位置。

上面這個命令的意思是說新增addr列到user1這一列後面:
alter table t1 add column addr varchar(20) not null after user1;

刪除資料庫:drop database mydb

CRUD:增刪改查
C:create 增加資料(必須要寫,可以為空)
insert into nation values('n001','漢族') -普通
insert into Name values('','')自增長列不用填,空著,不用填空字元
insert into nation (Code ,Name) values('n002','huizu')-往表中新增特定列的資料

R:read 查詢資料
一、查詢所有資料
select * from info 查所有
select Name from info 查特定列
二、根據條件查詢
select * from info where Code='p001'
select * from info where Code='p001' and nation='n003' 多條件 並關係 查詢
select * from info where name='胡軍' or nation='n001' 多條件 或關係 查詢
select * from car where price>=50 and price<=60 範圍查詢
select * from car where price between 50 and 60 範圍查詢常用
三、模糊查詢
select * from car where name like '%奧迪%' %萬用字元 代表任意多個字元

select * from car where name like '%奧迪%' or name like '%寶馬%'
select * from car where name like '_馬%' _萬用字元 代表任意一個字元
四、排序
select * from car order by price (asc) 按照價格升序排列
select * from car order by price desc 按照價格降序排列
select * from car order by price desc,oil desc 按照兩列進行排序-先按照價格降序排列,再按照油耗降序排列
五、統計函式(聚合函式)
select count(code) from car 查詢表中有多少條資料
select max(price) from car 取價格最大值
select min(price) from car 取價格最小值
select sum(price) from car 取價格總和
select avg(price) from car 取價格平均值
六、分組查詢
select brand from car group by brand having count(*) >2 查詢所有系列中數量大於2的
七、分頁查詢
select * from car limit 5,5 跳過幾條資料取幾條資料
八、去重查詢
select distinct brand from car 可以去哪一列的重複

 


U:update 修改資料
update nation set Name='huizu'-全部修改
update nation set Name='huizu' where Code='n002'-修改某一條資料
update nation set Name='huizu' where Code='n003' and Code='n004'-修改某一條資料

D:delete 刪除資料
delete from nation-刪除整個表中的資料
delete from nation where Code='n001'-刪除一條資料

高階查詢

一、多表連線
1.select * from Info,Nation where Info.Nation=Nation.Code
select Info.Code,Info.Name,Nation.Name from Info,Nation where Info.Nation=Nation.Code
2、join連線
select * from Info join Nation on Info.Nation=Nation.Code
二、多表聯合(列的擴充套件)
select * from Info where Code='p001'
union
select * from Info where Nation='n001'
三、子查詢(無關子查詢)
select * from Info where Nation (!)= (select Code from Nation where Name='漢族')
select * from Info where Nation (not)in (select Code from Nation where Name='漢族' or Name='苗族')
四、子查詢(相關子查詢)
select * from Car a where a.Oil<(select avg(Oil) from Car b where b.brand=a.brand)
第一次查詢裡層查詢變為 select avg(Oil) From car where brand='b001'

相關文章