多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 數據庫 > 數據庫應用 > QL之-建庫、建表、建約束、關系SQL基本語句

QL之-建庫、建表、建約束、關系SQL基本語句

來源:程序員人生   發布時間:2017-01-12 11:54:13 閱讀次數:3455次
</pre><pre name="code" class="sql"><pre name="code" class="sql">SQL之-建庫、建表、建束縛、關系SQL基本語句大全.txt舉得起放得下叫舉重,舉得起放不下叫負重。頭要有勇氣,抬頭要有底氣。學習要加,自豪要減,機會要乘,怠惰要除。人生3困難:思,相思,單相思。 SQL之-建庫、建表、建束縛、關系、部份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:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf.mdf',size=3mb,maxsize=unlimited,filegrowth=1) --建庫 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:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf.mdf',size=3mb,maxsize=unlimited,filegrowth=1) log on (name=N'wf',filename=N'E:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf_log.ldf',size=3mb,maxsize=unlimited,filegrowth=1) go use wf go if exists(select * from sys.sysobjects where name='wf' ) begin drop table wf end --建表、建束縛、關系 use wf go create table tableok ( col1 int, col2_notnull int not null, col3_default nchar(1) not null default('男'), --默許男 col4_default datetime not null default(getdate()), --默許得到系統時間 col5_check int not null check(col5_check>=18 and col5_check<=55), --添加束縛,數據值在18到55之間 col6_check nchar(9) not null check(col6_check like 'msd0902[0⑼][^6⑼]'), --添加束縛,數據值前7位必須是‘msd0902’,倒數第兩位可以是0⑼中任意1個數字,最后1位不是6⑼之間的數字。 cola_primary nchar(5) not null primary key, --建立主鍵 colb_unique int unique, --唯1束縛 col7_Identity int not null identity(100,1), --自增長,從100開始,每列值增加1個 col8_identity numeric(5,0) not null identity(1,1) --自增長,從1開始,每列值增加1個,最大值是5位的整數 col9_guid uniqueidentifier not null default(newid()) --使用newid()函數,隨機獲得列值 ) --alter --主外鍵/援用/關系 束縛 alter table 從表名 [with check]--啟用 with nocheck--禁用束縛 add constraint FK_主表名_從表名 foreign key (從表中的字段名) references 主表名 (主表中的字段名) --其它非主外鍵束縛 alter table wf add constraint 束縛名 束縛類型 具體的束縛說明 alter table wf--修改聯合主鍵 add constraint Pk_cola_primary primary key(cola_primary,col1) --⑴.insert 【into】 <表名>【列名】 values <值列表> -⑴)全部【列名】 可以省略 insert stuinfo values('','','') create table outtable ( id int not null primary key, [name] nvarchar(4) not null, adrress nvarchar(20) default '地址不詳' ) truncate table outtable alter table outtable alter column id int identity(4,1) insert outtable values (1,22,'') insert outtable values(2,32,'') insert outtable values(3,33,'') -⑵)部份 insert stuinfo (stuno) values ('') -⑶)自動增長列,不能手動插入。將列名、列值省略 --⑵多行插入 --⑴)從1個現有表中取出所取字段插入到目標表中 insert into stuinfo (stuno,stuname) select stuno,stuname from stumarks insert into stuinfo(stuno) select stuno from stumarks --⑵)從現有的表中取出數據,創建1個新表,將數據插入到新表中 select [列名] into <表名> from <源表名> select id as '編號',[name] into newtable from outtable select cast(id as varchar(4))+cast([name] as nvarchar(4)) as '編號及姓名' ,id into newtable from outtable select * from newtable drop table newtable --⑶)union 現有的多個表中取數據放入現有的表3中 insert into<表名3> [列名] select * from 表1 union select * from 表2 -⑶.更新語句 update <表名> set <列名=更新值> [where <更新條件>] ---注意:1般where不要省略,如不寫將修改全部表 -⑷truncate刪除數據 --比delete只能全部刪除數據,不能部份刪除,刪除的效力高,可以重置自增長列 select * from stumarks select * from stuinfos --給考試成績各提5分,100分封頂。 update stumarks set writtenexam=100 where writtenexam>95 update stumarks set labexam=100 where labexam>95 update stumarks set writtenexam=writtenexam+5 where writtenexam+5<=100 update stumarks set labexam=labexam+5 where labexam+5<=100 select examno,stuno,writtenexam+5 as 筆試,labexam+5 as 機試 from stumarks where writtenexam+5<=100 and labexam+5<=100 create 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 t select top 2 * from t select top 60 percent * from t select top 5 * from products select top 5 percent * from products --啟別名:兩種方式 As(可省略) 、= select productid as '編號' from products select productid '編號' from products select '編號'=productid from products select employeeid from employees --declare @a nvarchar(6) --set @a='員工編號為:' select N'員工編號為:'+cast(employeeid as nvarchar(2)) from employees select distinct country from suppliers order by country select * from t select * from products select * from products order by 4 asc,6 desc select * from products where unitprice>16 and productname like 'T%' or productid=16 select * from suppliers where country in('japan','italy') (1)char、varchar、text和nchar、nvarchar、ntext char和varchar的長度都在1到8000之間,它們的區分在于char是定長字符數據,而varchar是變長字符數據。所謂定長就是長度固定的,當輸入的數據長度沒有到達指定的長度時將自動以英文空格在其后面填充,使長度到達相應的長度;而變長字符數據則不會以空格填充。text存儲可變長度的非Unicode數據,最大長度為2^31⑴(2,147,483,647)個字符。 后面3種數據類型和前面的相比,從名稱上看只是多了個字母"n",它表示存儲的是Unicode數據類型的字符。寫進程序的朋友對Unicode應當很了解。字符中,英文字符只需要1個字節存儲就足夠了,但漢字眾多,需要兩個字節存儲,英文與漢字同時存在時容易造成混亂,Unicode字符集就是為了解決字符集這類不兼容的問題而產生的,它所有的字符都用兩個字節表示,即英文字符也是用兩個字節表示。nchar、nvarchar的長度是在1到4000之間。和char、varchar比較:nchar、nvarchar則最多存儲4000個字符,不論是英文還是漢字;而char、varchar最多能存儲8000個英文,4000個漢字。可以看出使用nchar、nvarchar數據類型時不用擔心輸入的字符是英文還是漢字,較為方便,但在存儲英文時數量上有些損失。 (2)datetime和smalldatetime datetime:從1753年1月1日到9999年12月31日的日期和時間數據,精確到百分之3秒。 smalldatetime:從1900年1月1日到2079年6月6日的日期和時間數據,精確到分鐘。 (3)bitint、int、smallint、tinyint和bit bigint:從⑵^63(⑼223372036854775808)到2^63⑴(9223372036854775807)的整型數據。 int:從⑵^31(⑵,147,483,648)到2^31⑴(2,147,483,647)的整型數據。 smallint:從⑵^15(⑶2,768)到2^15⑴(32,767)的整數數據。 tinyint:從0到255的整數數據。 bit:1或0的整數數據。 (4)decimal和numeric 這兩種數據類型是等效的。都有兩個參數:p(精度)和s(小數位數)。p指定小數點左側和右側可以存儲的10進制數字的最大個數,p必須是從 1到38之間的值。s指定小數點右側可以存儲的10進制數字的最大個數,s必須是從0到p之間的值,默許小數位數是0。 (5)float和real float:從⑴.79^308到1.79^308之間的浮點數字數據。 real:從⑶.40^38到3.40^38之間的浮點數字數據。在SQL Server中,real的同義詞為float(24)。 SELECT --從數據庫表中檢索數據行和列   select col1,col2,col3.....from table INSERT --向數據庫表添加新數據行 insert into table(col1,col2....) values(value1,value2...) insert into table(col1,col2....) select col1,col2...from table DELETE --從數據庫表中刪除數據行 delete from table UPDATE --更新數據庫表中的數據 update table set col1=value,...... --數據定義 CREATE TABLE --創建1個數據庫表 create table tbame (col1 int,col2 char(20)) DROP TABLE --從數據庫中刪除表 drop table tbname ALTER TABLE --修改數據庫表結構 --增加列 alter table #a add col3 int --刪除列 alter table #a drop column col3 --增加列的默許值 alter table tbname add CONSTRAINT tf_name default 'a' for col2 CREATE VIEW --創建1個視圖   create view view_name as select * from tbname  where .... DROP VIEW --從數據庫中刪除視圖 drop view view_nameCREATE INDEX --為數據庫表創建1個索引 --創建簡單的非聚集索引    CREATE INDEX index_name     ON tbname (VendorID); --創建簡單的唯1非聚集索引     CREATE UNIQUE INDEX index_name      ON tbname (VendorID); DROP INDEX --從數據庫中刪除索引 drop index index_name on tbnameCREATE PROCEDURE --創建1個存儲進程 create procedure name as begin ...... end DROP PROCEDURE --從數據庫中刪除存儲進程 drop procedure nameCREATE TRIGGER --創建1個觸發器 create trigger name on tbname FOR INSERT,UPDATE AS ....... DROP TRIGGER --從數據庫中刪除觸發器


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲精品美女久久久久 | 特黄色一级毛片 | 欧美x性 | 校园春色在线视频 | 国产精品女上位在线观看 | 亚洲v日本 | japanesevideo国产在线 | 欧美日韩在线永久免费播放 | 欧美第八页 | 亚洲国产一区视频 | 欧美一级aa天码毛片 | 国产欧美日韩图片一区二区 | 成人私拍福利视频在线 | 日韩在线高清视频 | 五月婷婷伊人 | 国产精品日韩欧美一区二区三区 | 国产亚洲精品美女一区二区 | 亚洲精品成人a在线观看 | 亚洲精品区一区二区三区四 | 手机看片国产免费久久网 | 欧美高清在线视频在线99精品 | 一级做a爱 一区 | 欧美一级视频免费观看 | 磁力天堂网在线资源www | 国产欧美日韩精品一区二区三区 | 国产中文字幕在线视频 | 亚洲一区二区欧美日韩 | 亚洲精品国产三级在线观看 | 精品视频一区二区三区免费 | 一二三四视频社区在线中文 | jjzz黄色| 宇都宫紫苑在线 | 亚洲偷偷 | 爱爱三级 | 成人久久久精品乱码一区二区三区 | 亚洲国产成人久久 | 国产成人精品一区二区 | 男女爽爽无遮挡午夜视频在线观看 | 和同事激情中文版在线观看 | 国产成人精品福利网站在线 | 免费亚洲视频在线观看 |