1. 程式人生 > >SQL Server基本命令

SQL Server基本命令

首先請允許我吐槽一下本科資料庫教材《資料庫系統概論》,最ZZ的莫過於我想把這本書實現一遍。

操作表:

Student (S#,Sname, Ssex,Sage,D#,Sclass)

SC(S#,C#,Score)

Course(C#,Cname,Chours,Credit,T#)

Teacher(T#,Tname,D#,Salary)

Dept(D#,Dname,Dean)

create:建立資料庫或表

create database SCT    --建立資料庫

create table SC(       --建立表
S# char(8),
C# char(3),
Score int
)

select:選取資料

select Sname from Student

select * from Student    -- 顯示Student表中所有列的資訊

distinct:去重

select distinct Sname from Student

where:按條件選擇

select Sname from Student
where Ssex = '女'

and 和 or

select Sname from Student
where Ssex = '女' and Sage < 20

select Sname from Student
where Ssex = '女' or Sage < 20

odrder by:對結果進行排序

select S#, Sname from Student
order by Sage asc       -- 按升序進行排序,降序desc

insert:插入

insert into Student values('98030101','張三','男',20,'03','980301')

insert into Student(S#, Sname, Ssex) values('98030102','張四','男')

updata:更新

update Student
set D# = '05',Ssex = '女' where Sname = '趙四'

delete:刪除

delete from Student
where Sname = '張三'

delete * from Student    -- 刪除Student中所有行

like:模糊查詢

select S#, Sname from Student
where Sname like '張%'    -- 尋找叫張某某的學生,'%'匹配零個或多個字元

select S#, Sname from Student
where Sname like '張_'    -- 尋找叫張某的同學,'_'匹配單個字元

select S#, Sname from Student
where Sname like '張\%'    -- 尋找叫張%的同學,'\'錶轉義字元

select S#, Sname from Student
where Sname like '[張]%'    -- 尋找以張開頭的同學

select S#, Sname from Student
where Sname like '[^張]%'    -- 尋找不是以張開頭的同學

in:允許在 where 子句中規定多個值

select distinct S# from SC    -- 列舉學過001號課程和002號課程同學的學號
where C# in ('001','002');

as:別名

select Sname as 姓名 from Student

inner join:內連線

select * from Student
inner join SC on Student.S# = SC.S#

left join:左連線

select * from Student
left join SC on Student.S# = SC.S#

right join:右連線 

select * from Student
right join SC on Student.S# = SC.S#

full join:全連線

select * from Student
full join SC on Student.S# = SC.S#

Union:並

select Sname from Student, SC where Student.S# = SC.S# and C# = '001'
union
select Sname from Student, SC where Student.S# = SC.S# and C# = '002'

-- Union後面加all 列出所有值(包括重複)

Intersect:交

select Sname from Student, SC where Student.S# = SC.S# and C# = '001'
intersect
select Sname from Student, SC where Student.S# = SC.S# and C# = '002'

except:差 

select distinct S# from SC
except
select S# from SC where C# = '002'

select into:批量複製

select Student.S#, Sname, Score into StudentBackup
from Student,SC
where Ssex = '女' and SC.S# = Student.S#

constraint:完整性約束

-- 列約束
create table tablename(colname datatype,    -- colname列的資料型別datatype

not null    -- 列值非空,表示若不向該欄位新增值,則無法插入或更新新紀錄

unique    -- 列值唯一,每個表可以有多個

primary key    -- 列為主鍵,每個表只能有一個,包含非空唯一特性

constraint constraintname    -- 約束命名為constraintname

check(search_cond)    -- 列值滿足條件search_cond,條件只能使用列當前值

references tablename (colname) on delete cascade | set null) -- 引用tablename的列colname的值,若引用表的某項被刪除,對應本表的對應項被刪除或者更新為null

-- 表約束
create table tablename(colname,colname,...),

constraint constraintname    -- 為約束名

unique(colname, colname,...)    -- 幾列值組合在一起是唯一

primary key(colname, colname, ...)    -- 幾列聯合為主鍵

check(search_condition)    -- 元組多列值共同滿足的條件,條件中只能使用同一元組的不同列當前值

foreign key(colname, colname,...)
reference tablename(colname, colname, ...)
on delete cascade | set null    -- 引用另一個表tablename的若干列的值作為外來鍵

drop:刪除

drop table Student    -- 刪除表
drop database SCT     -- 刪除資料庫
truncate table Student    -- 只刪除表的內容

alter:修改

alter table Student    -- 增加列
add Sheight float

alter table Student    -- 刪除列
drop column Sheight

alter table Student
alter column Sheight int    -- 修改列的屬性

view:檢視

create view test as        -- 建立檢視
select S#, Sname from Student

select * from test        -- 查詢檢視

select Sname from test    -- 在檢視的基礎上新增條件

drop view test            -- 撤銷檢視

group by:

select sum(Score) from SC        -- 每個學生的總成績
group by S#

select sum(Score) from SC        -- 每門課程的總成績
group by C#

having:

Select S#, Avg(Score) from SC    -- 有一門以上及格的同學的及格科目的平均成績
where Score > 60
group by S# having Count(*) > 1


select S#, Avg(Score) from SC        -- 有一門以上及格的同學的所有科目的平均成績
where S# in ( select S# from SC
where Score > 60
group by S# having Count(*) > 1)
group by S#;