国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 数据库 > 数据库应用 > QL之-建库、建表、建约束、关系SQL基本语句

QL之-建库、建表、建约束、关系SQL基本语句

来源:程序员人生   发布时间:2017-01-12 11:54:13 阅读次数:2914次
</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 --从数据库中删除触发器


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生