必須瞭解的sql語句 ,從基礎開始到深度理解 !(備註:要從上往下依次執行)
–建立表practice create table practice( id varchar(11),–員工ID name varchar(11),–員工名稱 age int,–年齡 department varchar(11),–部門 entrytime date –入職時間 )
–為表practice新增資料 insert into practice values(‘舒BUG153’,’李超朋’,’23’,’工程部’,GETDATE()) insert into practice values(‘舒BUG156’,’白長飛’,’32’,’財務部’,GETDATE()) insert into practice values(‘舒BUG213’,’袁豫雪’,’19’,’技術部’,GETDATE()) insert into practice values(‘舒BUG167’,’武亮’,’25’,’開發部’,GETDATE()) insert into practice values(‘舒BUG321’,’汪可’,’25’,’人事部’,GETDATE())
–建立表ecm create table ecm( adid varchar(11), –員工編號 adname varchar(11), –員工姓名 adage int, –年齡 )
————————————————————-新增以及刪除—————————————————————— insert into ecm(adid,adname,adage) select id,name,age from practice –將現有表裡的資料轉移到新表裡面
delete from ecm where adname=’李超朋’ –刪除姓名是李超朋的一行資料
truncate table practice –刪除表中的全部資料
delete practice –刪除表中的全部資料
drop table practice –刪除表以及表中的資料
alter table practice add pay int –新增薪資列名pay(薪資)
delete practice where id = ‘舒BUG153’ –按員工id條件刪除
insert into practice values(‘豫HPU123’,’擺渡’,34,’人事部’,getdate()-365*2,5000) –新增一條資料,入職時間減去兩年
insert into practice(id,name) values(‘豫HPU579’,’紅旗’) –只新增員工id和名字
————————————————————–查詢語句——————————————————————————— select top 2 * from practice –查詢前兩條資料
select adid,adage from ecm where adname=’白長飛’–只顯示白長飛的adid和adage
select adname as 姓名 from ecm where adage=’26’ –列名adname變成姓名
select name from practice where age in (32,19)–顯示年齡為32或19的名字
select * from practice where age between 18 and 20 –顯示年齡在18到20之間的員工資訊
select * from practice where name like ‘白%’ –查詢員工姓名中第一個字是‘白’的資訊
select * from practice where name like ‘%白’ –查詢員工姓名中最後的字是‘白’的人的資訊
select * from practice where name like ‘%白%’ –查詢員工名字中含有‘白’的人的資訊
select * from practice where age>=10 order by id –查詢年齡大於10的人,並按照ID升序的排列
select * from practice where age>=10 order by id desc –查詢年齡大於10的人,並按照ID降序序的排列
select top 3 * from practice order by id desc –查詢表中ID前三的資訊 根據ID進行降序
-兩表連線查詢
select * from practice as p inner join ecm as e on p.id = e.adid–內連線
select * from practice as p, ecm as e where p.id = e.adid–內連線
select * from practice as p left join ecm as e on p.id = e.adid –左連結
select * from practice as p right join ecm as e on p.id = e.adid –右連線
select * from practice as p full outer join ecm e on p.id = e.adid –全連線
———————————————————————修改———————————————————————————
update practice set pay=10000 –修改所有pay為10000
update ecm set adage=26 where adname=’白長飛’ –修改白長飛的年齡
update practice set pay = replace(pay,10000, 8000) –把pay為7000修改成8000
update practice set name = ‘李超’ where id = ‘舒BUG153’ –根據員工ID修改員工的名字
alter table practice alter column id int –修改欄位型別(如果要走下去最後可以在修改欄位型別)