SQL之-建庫、建表、建約束、關(guān)系SQL基本語句大全_第1頁
SQL之-建庫、建表、建約束、關(guān)系SQL基本語句大全_第2頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、sql之-建庫、建表、建約束、關(guān)系sql基本語句大全 sql之-建庫、建表、建約束、關(guān)系、部分t-sql語句 sql基本語句大全 sql之-建庫、建表、建約束、關(guān)系、部分t-sql語句-建庫 if exists(select * from sys.sysdatabases where name='wf') begin use master drop database wf end go create database wf on (name=n'wf',filename=n'e:mycodeetc收費(fèi)站etc收費(fèi)站etc_datawf.mdf',

2、size=3mb,maxsize=unlimited,filegrowth=1)-建庫if exists(select * from sys.sysdatabases where name='wf')beginuse masterdrop database wfendgocreate database wfon(name=n'wf',filename=n'e:mycodeetc收費(fèi)站etc收費(fèi)站etc_datawf.mdf',size=3mb,maxsize=unlimited,filegrowth=1)log on(name=n'wf&

3、#39;,filename=n'e:mycodeetc收費(fèi)站etc收費(fèi)站etc_datawf_log.ldf',size=3mb,maxsize=unlimited,filegrowth=1)gouse wfgoif exists(select * from sys.sysobjects where name='wf' )begindrop table wfend-建表、建約束、關(guān)系use wfgocreate table tableok(col1 int,col2_notnull int not null,col3_default nchar(1) not n

4、ull default('男'), -默認(rèn)男col4_default datetime not null default(getdate(), -默認(rèn)得到系統(tǒng)時(shí)間col5_check int not null check(col5_check=18 and col5_check=55), -添加約束,數(shù)據(jù)值在18到55之間col6_check nchar(9) not null check(col6_check like 'msd09020-96-9'), -添加約束,數(shù)據(jù)值前7位必需是msd0902,倒數(shù)第兩位可以是0-9中任意一個(gè)數(shù)字,最終一位不是6-9之間

5、的數(shù)字。cola_primary nchar(5) not null primary key, -建立主鍵colb_unique int unique, -唯一約束col7_identity int not null identity(100,1), -自增長,從100開頭,每列值增加1個(gè)col8_identity numeric(5,0) not null identity(1,1) -自增長,從1開頭,每列值增加1個(gè),最大值是5位的整數(shù)col9_guid uniqueidentifier not null default(newid() -使用newid()函數(shù),隨機(jī)獵取列值)-alter

6、-主外鍵/引用/關(guān)系 約束alter table 從表名 with check-啟用 with nocheck-禁用約束add constraint fk_主表名_從表名foreign key (從表中的字段名) references 主表名 (主表中的字段名)-其它非主外鍵約束alter table wfadd constraint 約束名 約束類型 詳細(xì)的約束說明alter table wf-修改聯(lián)合主鍵add constraint pk_cola_primary primary key(cola_primary,col1)-1.insert 【into】 表名【列名】 values 值列

7、表-1)全部【列名】 可以省略insert stuinfo values('','','')create table outtable(id int not null primary key,name nvarchar(4) not null,adrress nvarchar(20) default '地址不詳')truncate table outtablealter table outtable alter column id int identity(4,1)insert outtable values (1,22,'

8、;')insert outtable values(2,32,'')insert outtablevalues(3,33,'')-2)部分insert stuinfo (stuno) values ('')-3)自動(dòng)增長列,不能手動(dòng)插入。將列名、列值省略-2多行插入-1)從一個(gè)現(xiàn)有表中取出所取字段插入到目標(biāo)表中insert into sql之-建庫、建表、建約束、關(guān)系、部分t-sql語句 sql基本語句大全 stuinfo (stuno,stuname) select stuno,stuname from stumarks insert

9、into stuinfo(stuno) select stuno from stumarks-2)從現(xiàn)有的表中取出數(shù)據(jù),創(chuàng)建一個(gè)新表,將數(shù)據(jù)插入到新表中select 列名 into 表名 from 源表名select id as '編號(hào)',name into newtable from outtableselect cast(id as varchar(4)+cast(name as nvarchar(4) as '編號(hào)及姓名' ,id into newtable from outtableselect * from newtabledrop table new

10、table-3)union 現(xiàn)有的多個(gè)表中取數(shù)據(jù)放入現(xiàn)有的表3中insert into表名3 列名select * from 表1 unionselect * from 表2-3.更新語句 update 表名 set 列名=更新值 where 更新條件-留意:一般where不要省略,如不寫將修改整個(gè)表-4truncate刪除數(shù)據(jù)-比delete只能全部刪除數(shù)據(jù),不能部分刪除,刪除的效率高,可以重置自增長列select * from stumarksselect * from stuinfos-給考試成果各提5分,100分封頂。update stumarks set writtenexam=10

11、0where writtenexam95update stumarks set labexam=100where labexam95update stumarksset writtenexam=writtenexam+5where writtenexam+5=100update stumarksset labexam=labexam+5where labexam+5=100select examno,stuno,writtenexam+5 as 筆試,labexam+5 as 機(jī)試 from stumarks where writtenexam+5=100 and labexam+5=100c

12、reate table t(id int ,name nchar(4),dt datetime,age int,score int)insert t values (1,'a',getdate(),20,50)insert t values (2,'b',getdate(),21,60)insert t values (3,'c',getdate(),21,100)insert t values (4,'d',getdate(),23,80)select * from tselect top 2 * from tselect to

13、p 60 percent * from tselect top 5 * from productsselect top 5 percent * from products-啟別名:兩種方式 as(可省略) 、=select productid as '編號(hào)' from productsselect productid '編號(hào)' from productsselect '編號(hào)'=productid from productsselect employeeid from employees-declare a nvarchar(6)-set a=&#

14、39;員工編號(hào)為:'select n'員工編號(hào)為:'+cast(employeeid as nvarchar(2) from employeesselect distinct country from suppliers order by countryselect * from tselect * from productsselect * from products order by 4 asc,6 descselect * from products where unitprice16 and productname like 't%' or pr

15、oductid=16select * from suppliers where country in('japan','italy')(1)char、varchar、text和nchar、nvarchar、ntextchar和varchar的長度都在1到8000之間,它們的區(qū)分在于char是定長字符數(shù)據(jù),而varchar是變長字符數(shù)據(jù)。所謂定長就是長度固定的,當(dāng)輸入的數(shù)據(jù)長度沒有達(dá)到指定的長度時(shí)將自動(dòng)以英文空格在其后面填充,使長度達(dá)到相應(yīng)的長度;而變長字符數(shù)據(jù)則不會(huì)以空格填充。text存儲(chǔ)可變長度的非unicode數(shù)據(jù),最大長度為231-1(2,147,483,

16、647)個(gè)字符。后面三種數(shù)據(jù) sql之-建庫、建表、建約束、關(guān)系、部分t-sql語句 sql基本語句大全 類型和前面的相比,從名稱上看只是多了個(gè)字母n,它表示存儲(chǔ)的是unicode數(shù)據(jù)類型的字符。寫過程序的伴侶對(duì)unicode應(yīng)當(dāng)很了解。字符中,英文字符只需要一個(gè)字節(jié)存儲(chǔ)就足夠了,但漢字眾多,需要兩個(gè)字節(jié)存儲(chǔ),英文與漢字同時(shí)存在時(shí)簡單造成混亂,unicode字符集就是為了解決字符集這種不兼容的問題而產(chǎn)生的,它全部的字符都用兩個(gè)字節(jié)表示,即英文字符也是用兩個(gè)字節(jié)表示。nchar、nvarchar的長度是在1到4000之間。和char、varchar比較:nchar、nvarchar則最多存儲(chǔ)40

17、00個(gè)字符,不論是英文還是漢字;而char、varchar最多能存儲(chǔ)8000個(gè)英文,4000個(gè)漢字??梢钥闯鲈L用nchar、nvarchar數(shù)據(jù)類型時(shí)不用擔(dān)憂輸入的字符是英文還是漢字,較為便利,但在存儲(chǔ)英文時(shí)數(shù)量上有些損失。 (2)datetime和smalldatetimedatetime:從1753年1月1日到9999年12月31日的日期和時(shí)間數(shù)據(jù),精確到百分之三秒。smalldatetime:從1900年1月1日到2079年6月6日的日期和時(shí)間數(shù)據(jù),精確到分鐘。(3)bitint、int、smallint、tinyint和bitbigint:從-263(-9223372036854775

18、808)到263-1(9223372036854775807)的整型數(shù)據(jù)。int:從-231(-2,147,483,648)到231-1(2,147,483,647)的整型數(shù)據(jù)。smallint:從-215(-32,768)到215-1(32,767)的整數(shù)數(shù)據(jù)。tinyint:從0到255的整數(shù)數(shù)據(jù)。bit:1或0的整數(shù)數(shù)據(jù)。(4)decimal和numeric這兩種數(shù)據(jù)類型是等效的。都有兩個(gè)參數(shù):p(精度)和s(小數(shù)位數(shù))。p指定小數(shù)點(diǎn)左邊和右邊可以存儲(chǔ)的十進(jìn)制數(shù)字的最大個(gè)數(shù),p必需是從 1到38之間的值。s指定小數(shù)點(diǎn)右邊可以存儲(chǔ)的十進(jìn)制數(shù)字的最大個(gè)數(shù),s必需是從0到p之間的值,默認(rèn)小數(shù)位

19、數(shù)是0。(5)float和realfloat:從-1.79308到1.79308之間的浮點(diǎn)數(shù)字?jǐn)?shù)據(jù)。real:從-3.4038到3.4038之間的浮點(diǎn)數(shù)字?jǐn)?shù)據(jù)。在sql server中,real的同義詞為float(24)。select -從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列select col1,col2,col3.from tableinsert -向數(shù)據(jù)庫表添加新數(shù)據(jù)行insert into table(col1,col2.) values(value1,value2.)insert into table(col1,col2.) select col1,col2.from tabledelete -從數(shù)據(jù)庫表中刪除數(shù)據(jù)行delete from tableupdate -更新數(shù)據(jù)庫表中的數(shù)據(jù)update table set col1=value,.-數(shù)據(jù)定義create table -創(chuàng)建一個(gè)數(shù)據(jù)庫表create table tbame (col1 int,col2 char(20)drop table -從數(shù)據(jù)庫中刪除表drop table tbnamealter table -修改數(shù)據(jù)庫表結(jié)構(gòu)-增加列alter table #a add col3 int-刪除列alter table #a drop

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論