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

来源:互联网 发布:淘宝调货时间 编辑:程序博客网 时间:2024/05/16 05:54
</pre><pre name="code" class="sql"><pre name="code" class="sql">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=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 wfendgocreate database wfon (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 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 'msd0902[0-9][^6-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<表名3> [列名] 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 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-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:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据。int:从-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型数据。smallint:从-2^15(-32,768)到2^15-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.79^308到1.79^308之间的浮点数字数据。real:从-3.40^38到3.40^38之间的浮点数字数据。在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); DROP INDEX --从数据库中删除索引       drop index index_name on tbnameCREATE PROCEDURE --创建一个存储过程    create procedure name as    begin    ......    endDROP PROCEDURE --从数据库中删除存储过程     drop procedure nameCREATE TRIGGER --创建一个触发器    create trigger name on tbname        FOR INSERT,UPDATE     AS        .......DROP TRIGGER --从数据库中删除触发器  


0 0