日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > 數(shù)據(jù)庫 > 數(shù)據(jù)庫應用 > QL之-建庫、建表、建約束、關系SQL基本語句

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

來源:程序員人生   發(fā)布時間:2017-01-12 11:54:13 閱讀次數(shù):3461次
</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()), --默許得到系統(tǒng)時間 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 'msd0902[0⑼][^6⑼]'), --添加束縛,數(shù)據(jù)值前7位必須是‘msd0902’,倒數(shù)第兩位可以是0⑼中任意1個數(shù)字,最后1位不是6⑼之間的數(shù)字。 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位的整數(shù) col9_guid uniqueidentifier not null default(newid()) --使用newid()函數(shù),隨機獲得列值 ) --alter --主外鍵/援用/關系 束縛 alter table 從表名 [with check]--啟用 with nocheck--禁用束縛 add constraint FK_主表名_從表名 foreign key (從表中的字段名) references 主表名 (主表中的字段名) --其它非主外鍵束縛 alter table wf add constraint 束縛名 束縛類型 具體的束縛說明 alter table wf--修改聯(lián)合主鍵 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個現(xiàn)有表中取出所取字段插入到目標表中 insert into stuinfo (stuno,stuname) select stuno,stuname from stumarks insert into stuinfo(stuno) select stuno from stumarks --⑵)從現(xiàn)有的表中取出數(shù)據(jù),創(chuàng)建1個新表,將數(shù)據(jù)插入到新表中 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 現(xiàn)有的多個表中取數(shù)據(jù)放入現(xiàn)有的表3中 insert into<表名3> [列名] select * from 表1 union select * from 表2 -⑶.更新語句 update <表名> set <列名=更新值> [where <更新條件>] ---注意:1般where不要省略,如不寫將修改全部表 -⑷truncate刪除數(shù)據(jù) --比delete只能全部刪除數(shù)據(jù),不能部份刪除,刪除的效力高,可以重置自增長列 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之間,它們的區(qū)分在于char是定長字符數(shù)據(jù),而varchar是變長字符數(shù)據(jù)。所謂定長就是長度固定的,當輸入的數(shù)據(jù)長度沒有到達指定的長度時將自動以英文空格在其后面填充,使長度到達相應的長度;而變長字符數(shù)據(jù)則不會以空格填充。text存儲可變長度的非Unicode數(shù)據(jù),最大長度為2^31⑴(2,147,483,647)個字符。 后面3種數(shù)據(jù)類型和前面的相比,從名稱上看只是多了個字母"n",它表示存儲的是Unicode數(shù)據(jù)類型的字符。寫進程序的朋友對Unicode應當很了解。字符中,英文字符只需要1個字節(jié)存儲就足夠了,但漢字眾多,需要兩個字節(jié)存儲,英文與漢字同時存在時容易造成混亂,Unicode字符集就是為了解決字符集這類不兼容的問題而產(chǎn)生的,它所有的字符都用兩個字節(jié)表示,即英文字符也是用兩個字節(jié)表示。nchar、nvarchar的長度是在1到4000之間。和char、varchar比較:nchar、nvarchar則最多存儲4000個字符,不論是英文還是漢字;而char、varchar最多能存儲8000個英文,4000個漢字。可以看出使用nchar、nvarchar數(shù)據(jù)類型時不用擔心輸入的字符是英文還是漢字,較為方便,但在存儲英文時數(shù)量上有些損失。 (2)datetime和smalldatetime datetime:從1753年1月1日到9999年12月31日的日期和時間數(shù)據(jù),精確到百分之3秒。 smalldatetime:從1900年1月1日到2079年6月6日的日期和時間數(shù)據(jù),精確到分鐘。 (3)bitint、int、smallint、tinyint和bit bigint:從⑵^63(⑼223372036854775808)到2^63⑴(9223372036854775807)的整型數(shù)據(jù)。 int:從⑵^31(⑵,147,483,648)到2^31⑴(2,147,483,647)的整型數(shù)據(jù)。 smallint:從⑵^15(⑶2,768)到2^15⑴(32,767)的整數(shù)數(shù)據(jù)。 tinyint:從0到255的整數(shù)數(shù)據(jù)。 bit:1或0的整數(shù)數(shù)據(jù)。 (4)decimal和numeric 這兩種數(shù)據(jù)類型是等效的。都有兩個參數(shù):p(精度)和s(小數(shù)位數(shù))。p指定小數(shù)點左側(cè)和右側(cè)可以存儲的10進制數(shù)字的最大個數(shù),p必須是從 1到38之間的值。s指定小數(shù)點右側(cè)可以存儲的10進制數(shù)字的最大個數(shù),s必須是從0到p之間的值,默許小數(shù)位數(shù)是0。 (5)float和real float:從⑴.79^308到1.79^308之間的浮點數(shù)字數(shù)據(jù)。 real:從⑶.40^38到3.40^38之間的浮點數(shù)字數(shù)據(jù)。在SQL Server中,real的同義詞為float(24)。 SELECT --從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列   select col1,col2,col3.....from table INSERT --向數(shù)據(jù)庫表添加新數(shù)據(jù)行 insert into table(col1,col2....) values(value1,value2...) insert into table(col1,col2....) select col1,col2...from table DELETE --從數(shù)據(jù)庫表中刪除數(shù)據(jù)行 delete from table UPDATE --更新數(shù)據(jù)庫表中的數(shù)據(jù) update table set col1=value,...... --數(shù)據(jù)定義 CREATE TABLE --創(chuàng)建1個數(shù)據(jù)庫表 create table tbame (col1 int,col2 char(20)) DROP TABLE --從數(shù)據(jù)庫中刪除表 drop table tbname ALTER TABLE --修改數(shù)據(jù)庫表結(jié)構(gòu) --增加列 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 --創(chuàng)建1個視圖   create view view_name as select * from tbname  where .... DROP VIEW --從數(shù)據(jù)庫中刪除視圖 drop view view_nameCREATE INDEX --為數(shù)據(jù)庫表創(chuàng)建1個索引 --創(chuàng)建簡單的非聚集索引    CREATE INDEX index_name     ON tbname (VendorID); --創(chuàng)建簡單的唯1非聚集索引     CREATE UNIQUE INDEX index_name      ON tbname (VendorID); DROP INDEX --從數(shù)據(jù)庫中刪除索引 drop index index_name on tbnameCREATE PROCEDURE --創(chuàng)建1個存儲進程 create procedure name as begin ...... end DROP PROCEDURE --從數(shù)據(jù)庫中刪除存儲進程 drop procedure nameCREATE TRIGGER --創(chuàng)建1個觸發(fā)器 create trigger name on tbname FOR INSERT,UPDATE AS ....... DROP TRIGGER --從數(shù)據(jù)庫中刪除觸發(fā)器


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产三级三级三级精品8ⅰ区 | 91久久精品一区 | 国产精品久久久一区 | 免费视频爱爱太爽了 | 日韩私人影院 | 亚洲天堂视频在线观看 | 国产高潮在线观看 | 亚洲精品在线观看网站 | 国产精品污www在线观看 | 欧美久久一区二区 | 久久久蜜臀 | 亚洲成人福利 | 日韩欧美在线视频 | 久久大 | 日本一区二区三区视频在线播放 | 国产伦精品一区二区三区免费 | www亚洲精品 | 黄色a视频在线观看 | 国产成人高清视频 | 精品久久精品 | 日韩精品在线视频 | 亚洲永久在线 | 久久久久久九九 | 成人午夜电影网 | 亚洲精品成人久久 | 国产精品久久久久久亚洲伦 | 超碰总站 | 炮机高潮痉挛哭叫失禁 | 日韩精品免费一区二区夜夜嗨 | 韩国三级视频 | 欧美日韩在线一区 | 日韩欧美一卡二卡 | 欧美日韩亚洲不卡 | 激情欧美日韩一区二区 | 国产福利电影网 | 久草在线视频福利 | 久久久久久久久久久久久九 | 中国一级毛片 | 99久热| 视频精品一区二区三区 | 午夜精品一区二区三区在线播放 |