字符串用單引號',判斷用單等號=,兩個單引號''轉義為一個單引號'
不等號是<>不區分大小寫[]括起來的要不是關鍵字,要不是非法變量,比如空格隔起來的變量創建與刪除數據庫1 --創建數據庫 2 create database School; 3 --刪除數據庫 4 drop database School; 5 --創建數據庫的時候指定一些選項 6 create database School; 7 on PRimary--配置主數據文件 8 ( 9 name='School',--邏輯名稱,數據庫內部用的名字10 filename='',--保存路徑11 size=5MB,--設置初始化大小12 filegrowth=10MB或10%,--設置增長速度13 maxsize=100MB--設置最大大小14 )15 log on--配置主日志文件16 (17 name='',--設置邏輯名稱18 filename='',--設置路徑19 size=3MB,20 filegrowth=3%,21 maxsize=20MB22 )
1 --每個項設置后面都加逗號 最后一項不加 2 --切換數據庫 3 use School; 4 --在School數據庫中創建一個學生表 5 create table TblStudent 6 ( 7 --表中的定義在這對的小括號中 8 --開始創建列 9 --列名 數據類型 自動編號(從幾開始,增長步長) 是否為空(不寫默認允許,或者寫null 不允許空寫 not null)10 --tsid int identity(1,1) not null11 --設置名為tsid 類型為int 從1開始增長,每次增長1的主鍵列12 tsid int identity(1,1) primary key,13 )
1 --查詢表 2 select * from TblClass 3 --insert向表插入數據(一次只能插一條) 4 insert into TblClass(tclassName,tclassDesc) values('tclassName的值','tclassDesc的值') 5 --TblClass后面的括號設置在哪些列插入數據,value后面的括號要與前面的一一對應.如果要給除自動編號的所有列插入數據,TblClass后面的括號可省 6 --插入同時返回指定列的數據:在value前加上output inserted.列名 7 8 9 --向自動編號列插入數據10 --先把一個選項打開 倒數第二個是列名11 set IDENTITY_INSERT tblclass on12 insert........13 --最后記得把選項關掉14 --聽過一條語句插入多條數據15 insert into TblClass(tclassName,tclassDesc)16 select '...','...' union17 select '...','...' union18 select '...','...' union19 select '...','...' --最后一項不用union20 --把一個表的書數據插入另一個表21 insert into 被插表(列名,列名)22 select 列名,列名 from 數據來源表23 --插入漢字記得在字符串前加入N
1 update 表名 set 列名=值,列名2=值2 where 條件 and..or...2 --如果沒有條件,所有數據都會更新
1 --刪除 2 delete from 表名 where 條件 3 --刪除整表數!據!與drop不同,兩種 4 --1,delete from 表名 5 --速度慢 6 --自動編號依然保留當前已經增長的位置 7 delete from 表名 8 --2,truncate table 表名 9 --速度快10 --自動編號重置11 truncate table 表名
1 --修該列 2 --刪除指定列 3 alter table 表名 drop column 列名 4 --增加指定列 5 alter table 表名 add 列名(這里跟創建表一樣) 6 7 --修改指定列 8 alter table 表名 alter column 列名 9 --增加約束10 --給指定列添加主鍵約束11 alter table 表名 add constraint 約束名 primary key(列名)12 --給指定列添加非空約束13 alter table 表名 alter column 列名 數據類型 not null14 --給指定列添加唯一約束15 alter table 表名 add constrainy UQ開頭的約束名 unique(列名)16 --給指定列添加默認約束17 alter table 表名 add constraint 約束名 default(值) for 列名18 --給指定列添加檢查約束19 alter table 表名 add constraint 約束名 check(表達式) 20 --增加外鍵約束21 alter table 表名 add constraint FK_約束名 foreign key(外鍵列名)references 主鍵表名(列名)22 23 --刪除多個約束24 alter table 表名 drop constraint 約束名,...,...25 26 --創建多個約束27 alter table 表名 add28 constraint 約束名 unique(列名),29 constraint 約束名 check(表達式),30 constraint 約束名 foreign key(要引用列)31 references 被引用表(列) 32 on delete cascade on update cascade --設置級聯刪除和更新
1 --數據檢索,查詢指定列的數據,不加where返回所有2 select 列名,列名,... from 表名 where 條件3 --用select顯示東西,列名可省略,列名可以不''起來,除非名字有特殊符號4 select 值 (空格或者as) 列名5 --top獲得前幾條數據,選到的都是向上取整6 select top (數字或數字 percent) * from 表名 order by 列名 (asc//升序,默認值 desc//降序)7 --Distinct去除查詢出的重復數據,只要有一點不同(顯示出來的列的內容不同)不算重復,比如自動增長的那列8 select distinct 要顯示的列 from 表名 ...
合并行叫做"聯合"
聯合必須保證每行的數據數目與第一行一致,數據類型兼容
列名為第一行的列名
union all 在聯合時不會去除重復數據,也不自動排序
不能分別排序
常用:底部總和
例:
select
商品名稱,
銷售總價格=(sum(銷售數量*銷售價格))
fromMyOrders
groupby商品名稱
union all
select'銷售總價:',sum(銷售數量*銷售價格)fromMyOrders
1 select*from TblClass2 jion tblstudent on TblClass.tClassId=TblStudent.tSClassId3 --查詢出兩個表符合TblClass.tClassId=TblStudent.tSClassId的數據行,并顯示其所有數據
1 select*from TblClass2 jion tblstudent on TblClass.tClassId=TblStudent.tSClassId
1 select * from TestJoin1Emp emp inner join TestJoin2Dept dept on emp.EmpDeptid=dept.DeptId2 --兩者等效3 select * from TestJoin1Emp emp , TestJoin2Dept dept 4 --假設emp與dept,行數分別是3,5.select * from 這兩個表,實際會形成3*5=15行的臨時表5 --再在這個表中篩選,而這個表叫做笛卡爾表6 where7 emp.EmpDeptid=dept.DeptId
1 create table groups 2 ( 3 gid int identity(1,1) primary key not null, 4 gname nvarchar(10), 5 gparent int 6 ) 7 select * from groups 8 insert into groups values('總部',0) 9 insert into groups values('北京分公司',1)10 insert into groups values('上海分公司',1)11 insert into groups values('.net部門',2)12 insert into groups values('.net部門',3)13 --查詢部門對應的上級部門14 use Temp15 select deparment.gname as '部門名稱',comp
新聞熱點
疑難解答