SQL之-建库、建表、建约束、关系SQL基本语句大全.doc_第1页
SQL之-建库、建表、建约束、关系SQL基本语句大全.doc_第2页
SQL之-建库、建表、建约束、关系SQL基本语句大全.doc_第3页
SQL之-建库、建表、建约束、关系SQL基本语句大全.doc_第4页
SQL之-建库、建表、建约束、关系SQL基本语句大全.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SQL之-建库、建表、建约束、关系SQL基本语句大全.txt举得起放得下叫举重,举得起放不下叫负重。头要有勇气,抬头要有底气。学习要加,骄傲要减,机会要乘,懒惰要除。人生三难题:思,相思,单相思。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=Nwf,filename=NE:MyCodeETC收费站ETC收费站ETC_Datawf.mdf,size=3mb,maxsize=unlimited,filegrowth=1)-建库if exists(select * from sys.sysdatabases where name=wf)begin use master drop database wfendgocreate database wfon (name=Nwf,filename=NE:MyCodeETC收费站ETC收费站ETC_Datawf.mdf,size=3mb,maxsize=unlimited,filegrowth=1)log on (name=Nwf,filename=NE:MyCodeETC收费站ETC收费站ETC_Datawf_log.ldf,size=3mb,maxsize=unlimited,filegrowth=1)go use wfgoif 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 msd09020-96-9), -添加约束,数据值前7位必须是msd0902,倒数第两位可以是0-9中任意一个数字,最后一位不是6-9之间的数字。cola_primary nchar(5) not null primary key, -建立主键colb_unique int unique, -唯一约束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) -1.insert 【into】 【列名】 values -1)全部【列名】 可以省略 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,) -2)部分 insert stuinfo (stuno) values () -3)自动增长列,不能手动插入。将列名、列值省略 -2多行插入 -1)从一个现有表中取出所取字段插入到目标表中 insert into stuinfo (stuno,stuname) select stuno,stuname from stumarks insert into stuinfo(stuno) select stuno from stumarks -2)从现有的表中取出数据,创建一个新表,将数据插入到新表中 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 -3)union 现有的多个表中取数据放入现有的表3中 insert into 列名 select * from 表1 union select * from 表2 -3.更新语句 update set where -注意:一般where不要省略,如不写将修改整个表 -4truncate删除数据 -比delete只能全部删除数据,不能部分删除,删除的效率高,可以重置自增长列 select * from stumarks select * from stuinfos -给考试成绩各提5分,100分封顶。 update stumarks set writtenexam=100 where writtenexam95 update stumarks set labexam=100 where labexam95 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+516 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数据,最大长度为231-1(2,147,483,647)个字符。 后面三种数据类型和前面的相比,从名称上看只是多了个字母n,它表示存储的是Unicode数据类型的字符。写过程序的朋友对Unicode应该很了解。字符中,英文字符只需要一个字节存储就足够了,但汉字众多,需要两个字节存储,英文与汉字同时存在时容易造成混乱,Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示。nchar、nvarchar的长度是在1到4000之间。和char、varchar比较:nchar、nvarchar则最多存储4000个字符,不论是英文还是汉字;而char、varchar最多能存储8000个英文,4000个汉字。可以看出使用nchar、nvarchar数据类型时不用担心输入的字符是英文还是汉字,较为方便,但在存储英文时数量上有些损失。(2)datetime和smalldatetimedatetime:从1753年1月1日到9999年12月31日的日期和时间数据,精确到百分之三秒。smalldatetime:从1900年1月1日到2079年6月6日的日期和时间数据,精确到分钟。(3)bitint、int、smallint、tinyint和bitbigint:从-263(-9223372036854775808)到263-1(9223372036854775807)的整型数据。int:从-231(-2,147,483,648)到231-1(2,147,483,647)的整型数据。smallint:从-215(-32,768)到215-1(32,767)的整数数据。tinyint:从0到255的整数数据。bit:1或0的整数数据。(4)decimal和numeric这两种数据类型是等效的。都有两个参数:p(精度)和s(小数位数)。p指定小数点左边和右边可以存储的十进制数字的最大个数,p必须是从 1到38之间的值。s指定小数点右边可以存储的十进制数字的最大个数,s必须是从0到p之间的值,默认小数位数是0。(5)float和realfloat:从-1.79308到1.79308之间的浮点数字数据。real:从-3.4038到3.4038之间的浮点数字数据。在SQL Server中,real的同义词为float(24)。 SELECT -从数据库表中检索数据行和列 select col1,col2,col3.from tableINSERT -向数据库表添加新数据行 insert into table(col1,col2.) values(value1,value2.) insert into table(col1,col2.) select col1,col2.from tableDELETE -从数据库表中删除数据行 delete from table UPDATE -更新数据库表中的数据 update table set col1=value,.-数据定义 CREATE TABLE -创建一个数据库表 create table tbame (col1 int,col2 char(20)DROP TABLE -从数据库中删除表 drop table tbnameALTER 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 -创建一个视图 create view view_name as select * from tbname where .DROP VIEW -从数据库中删除视图 drop view view_nameCREATE INDEX -为数据库表创建一个索引 -创建简单的非聚集索引CREATE INDEX index_name ON tbname (VendorID); -创建简单的唯一非聚集索引CREATE UNIQUE INDEX index_name ON tbname (VendorID); DR

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论