sql 經典命令列建立資料庫 (增刪改查)
阿新 • • 發佈:2018-12-31
--主鍵:不允許為空值 create table gsbh ( bianhao int primary key,--主鍵為編號 xingming nvarchar(10), nianlin int ) --新增欄位記錄 注意沒有逗號, insert into gsbh values(1, '悟空1', 28) insert into gsbh values(2, '悟空2', 29) insert into gsbh values(3, '悟空3', 30) select * from gsbh //更改某個記錄 update gsbh set nianlin = 23 where nianlin = 29 update gsbh set xingming = '唐僧' where bianhao = 2 update gsbh set xingming = '八戒' where nianlin = 30 --刪除 delete from gsbh where nianlin = 30 and xingming = '八戒' --外來鍵只能指向主鍵並且型別完全一致 create table bumen ( bianhao int primary key, mingcheng nvarchar(5), didian nvarchar(5), ) create table renwu ( paihang int primary key, xingming nvarchar(10), zhiwu nvarchar(5), shangji int, ruzhishijian datetime, gongzi numeric(6,1), buzhu numeric(5,1), bianhao int foreign key references bumen(bianhao) ) delete from bumen drop table bumen insert into bumen values(1,'總頭領','聚義廳') insert into bumen values(2,'文職','前山') insert into bumen values(3,'馬軍','左山') insert into bumen values(4,'步軍','右山') insert into bumen values(5,'水軍','山下') insert into bumen values(6,'後勤','後山') select * from bumen select bianhao 編號 ,mingcheng 名稱,didian 地點 from bumen insert into renwu(paihang,xingming,zhiwu,ruzhishijian,gongzi,buzhu,bianhao) values (101,'宋江','寨主','2002-3-15',20009,5000,1) insert into renwu values (102,'宋江2','寨主2','101','2002-3-15',20008,5000,1) insert into renwu values (103,'宋江3','寨主3','102','2002-3-15',20007,4000,2) insert into renwu values (104,'宋江4','寨主4','103','2002-3-15',20006,3000,3) insert into renwu values (105,'宋江5','寨主5','104','2002-3-15',20005,2000,4) insert into renwu values (106,'宋江6','寨主6','105','2002-3-15',20004,1000,5) insert into renwu values (107,'宋江7','寨主7','106','2002-3-15',20003,500,6) insert into renwu values (108,'宋江8','寨主8','107','2002-3-15',20002,400,6) insert into renwu values (109,'宋江9','寨主9','108','2002-3-15',20001,300,6) delete from renwu select * from renwu select bianhao 編號 ,mingcheng 名稱,didian 地點 from bumen --查詢宋江7 select xingming ,bianhao ,gongzi from renwu where paihang = 107 --查詢編號有多少(不重複) select distinct bianhao xingming from renwu --顯示每個員工的姓名和年薪 select xingming 姓名, ruzhishijian 入職時間 ,gongzi * 12 + buzhu* 12 年薪 from renwu --查詢月薪補助小於3000的 select xingming 姓名,zhiwu 職位,gongzi 工資 ,buzhu 補助 from renwu where 1000<=buzhu and buzhu < 3000 --模糊查詢(進行姓名查詢 ,注意_ 一個’_‘代表一個字元) select xingming 姓名,zhiwu 職位,gongzi 工資 ,buzhu 補助 from renwu where xingming like '__8%' --批量查詢 select xingming 姓名,zhiwu 職位,gongzi 工資 ,buzhu 補助 from renwu where paihang in( 101,103,106,108,102) --顯示一把手的工資(上級為空) select xingming 姓名,zhiwu 職位,gongzi 工資 ,buzhu 補助 from renwu where shangji is null --排序預設是 升序 降序是desc 中文按照是拼音的順序 和 音調 Select xingming 姓名,zhiwu 職位,gongzi 工資,buzhu 補助 from renwu order by gongzi desc --升序排列工資 Select xingming 姓名,zhiwu 職位,gongzi 工資,buzhu 補助 from renwu order by gongzi --按照姓名排序(降序) Select xingming 姓名,zhiwu 職位,gongzi 工資,buzhu 補助 from renwu order by xingming desc --求最大的工資 select max(gongzi ) from renwu --求最小工資 select min(gongzi ) from renwu --求總工資,和平均工資 select sum(gongzi) '總工資', avg(gongzi)'平均工資' from renwu --查詢工資最多的人的姓名, Select xingming 姓名,zhiwu 職位,gongzi 工資,buzhu 補助 from renwu where gongzi > (select avg(gongzi ) from renwu) --將工資與平均工資對比(帶有函式的要用欄位標記起來) select xingming 姓名,zhiwu 職位,gongzi 工資,buzhu 補助,(select avg(gongzi)'平均工資' from renwu) from renwu where gongzi > (select avg(gongzi ) from renwu) --按照部門編號升序,但是要按照工資降序 select xingming 姓名,zhiwu 職位,gongzi * 12 + isnull(buzhu ,0) 年薪,buzhu 補助 from renwu order by bianhao, 年薪 desc --統計有多少條記錄 select count(*) from renwu -- select bianhao,(sum(gongzi) * 12) 年薪,( avg(gongzi) * 12) 平均工資 ,(min(gongzi) * 12) 最低年工資,zhiwu 名稱 from renwu group by bianhao,zhiwu order by bianhao --統計平均工資 大於20005的部門 select bianhao,( avg(gongzi) * 12) 平均工資 from renwu group by bianhao having avg(gongzi) < 20005 --多表查詢 --的笛卡爾集現象 select * from renwu,bumen select * from renwu,bumen where bumen.mingcheng = '水軍' and renwu.bianhao= bumen.bianhao --2張表的編號完全重合 select * from renwu,bumen where renwu.bianhao= bumen.bianhao --顯示姓名和所在部門和部門所在的編號 select xingming 姓名,mingcheng 名稱,renwu.bianhao 部門編號 from renwu, bumen where renwu.bianhao = bumen.bianhao --顯示姓名和所在部門和工資 和 部門編號 = 4的 select xingming 姓名,mingcheng 名稱,gongzi 工資,renwu.bianhao 部門編號 from renwu, bumen where renwu.bianhao = bumen.bianhao and bumen.bianhao = 4 --顯示部門編號,姓名,工資,並且按照部門編號進行排序 select a.xingming 姓名,b.xingming 上級 from renwu a,renwu b where a.shangji = b.paihang --顯示與宋江7同部門的姓名 select xingming 姓名,renwu.bianhao 部門 from renwu ,bumen where (renwu.bianhao = (select bianhao from renwu where xingming = '宋江7')) and (renwu.bianhao = bumen.bianhao)