ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:61.57KB ,
资源ID:4617841      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4617841.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(sql基本语句代码总结.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

sql基本语句代码总结.docx

1、sql基本语句代码总结1. 创建sql 语句Create database mydbon( Name=mydb_dat, Filename=f:datamydb_dat.mdf, Size=5mb, Maxsize=20mb, Filegrowth=5%)Log on( Name=mydb_log, Filename=f:datamydb_log.ldf, Size=5mb, Maxsize=20mb, Filegrowth=1mb)2. 可以创建同一个文件组,组名默认为PRIMARY,数据文件可以存放在不同磁盘上,数据库中的数组是分布在所有的次要文件中create database MyDB

2、 on primary( name=mydb_dat, filename=f:datamydb_dat.mdf, size=6MB, maxsize=20mb, filegrowth=5%),filegroup dbfg ( name=dbfg_dat1, filename=d:datamydb_dat1.ndf, size=5mb, maxsize=20mb, filegrowth=5%), ( name=dbfg_dat2, filename=e:datamydb_dat2.ndf, size=5mb, maxsize=20mb, filegrowth=1mb)log on( name=m

3、ydb_log, filename=f:datamydb_log.ldf, size=5MB, maxsize=20mb, filegrowth=5%) 3. 修改数据库名 alter database mydb modify name=youdb 存储过程修改数据库名 exec sp_dboption youdb,single,true exec sp_renamedb youdb,mydbexec sp_dboption mydb,single,false 4. 修改数据库文件大小Use masterAlter database mydb Modify file( Name=mydb_da

4、t, Size=6mb, Filegrowth=1%)5. 增加数据库文件组Use masterAlter database mydbAdd file( Filename=f:datamydb_data1.ndf, Name=mydb_dat1, Size=6mb, Filegrowth=1%)6. 删除数据库 drop database mydb7. 从sql 中删除,但保持其数据和事务日志文件(相当于分离数据库)Exec sp_detach_db mydb 8. 附加数据库 Exec sp_attach_db mydb,f:datamydb_dat.mdf9. 备份数据库Backup da

5、tabase mydb toDisk=f:datadatabase_bak.dat with name=backup10. 还原数据库restore database mydb from disk=f:datadatabase_bak.dat with replace11. 创建数据库快照create database mydb001On( name=mydb_dat, Filename=f:datamydb001.mdf),(name=ddd, -这是次要文件可以没有Filename=f:ddd.ndf)As snapshot of mydb12. 恢复数据库快照restore databa

6、se mydb from database snapshot=mydb00113. 查看日志 dbcc log (数据库名,n) N的值 0 - 最少信息(operation, context, transaction id) 1 - 更多信息(plus flags, tags, row length) 2 - 非常详细的信息(plus object name, index name,page id, slot id) 3 - 每种操作的全部信息 4 - 每种操作的全部信息加上该事务的16进制信息 默认 type = 01. 创建用户自定义类型sp_addtypetype_name,base_

7、type,null|not null,拥有者删除 sp_droptype type_name2. 在MMM数据库中创建自定义数据类型exec sp_addtype nametype,varchar(20),not null删除 sp_droptype nametype3. 创建临时表 l临时表在退出其作用域是由系统自动删除create table #mytemptable 一个#的为本地临时表( Cola int)create table #mytemp 两个#的为全局临时表( Cola int) 在tempdb 库中查看临时表信息4. 创建、删除表创建表sales 包含以下字段 order_

8、no 不能空 Ordr_date 不能为空 Ship_date 不能为空use projects Create table sales( Order_no int not null, order_date datetime not null, Ship_date datetime not null)更改表名Exec sp_rename sales ssaless删除表drop table sales5. .创建函数列的表Create table testtable(Indate as getdate(),Id int,Usename as user_name() . 用户定义类型列创建计算列c

9、reate table testable( id int not null, Col1 nametype). 表变量 declare t table ( col1 int, col2 varchar(10) Insert into t values(1,很好)查询 select * from t6. 修改表employee添加列:电话号码 telephone_no 添加列:字符列email为char修改列类型:email为varchar 修改列空为非空列 修改字段名: 删除列:删除email列alter table employee Add telephone int null,Email c

10、har(29) null Alter table eemployee alter column email varchar(20) null alter table employee alter column email varchar(20) not nullexec sp_rename “构架名.表名.旧字段名,新字段名,columnalter table employeedrop column email7. 约束. 主键约束 创建是加主键 create table customers( customerno int identity not null primary key, Cust

11、omername varchar(30) not null) 在现有表中添加主键 alter table customers Add constraint pk_customerno Primary key (customerno)外键约束 使用外键床架一个表 Create table orders ( Ordered int identity not null primary key , Customerno int not null foreign key references customers(customerno)在已存在的表中添加外键alter table orders add c

12、onstraint fk_employeeCreatesorders Foreign key (customerno) references customers (customerno)级联动作 create table OrderDetails ( orderid int not null, partno varchar(10) not null constraint PKOrderDetails primary key (orderid,partno), constraint FKOrderContrainsDetails Foreign key (OrderID) References

13、orders(orderid) on update no action on delete cascade) 唯一约束 在创建表时创建唯一约束 create talbe shippers ( Shipperid int identity not null primary key,Phoneno varchar(14) not null unique)在已存在的表中创建唯一约束alter table shippersAdd constraint ak_shippersphoneno unique(phoneno)Check 约束alter table customers Add constrai

14、nt cn_customerdateinsystem check(dateinsystem=getdate()删除check 约束alter table testableDrop constraint cn_customerdateinsystem. Default 约束 创建默认表 create table shippers( Shipperid int identity not null primary key, Dateinsystem smalldatetime not null default getdate () 在已存在的表上加上默认值 alter table customers

15、 add constraint cn_customerdefaultdateinsystemDefault getdate() for dateinsystem1. 表数据插入使用Insert 插入USE northwindINSERT into customers(customerid, companyname, contactname, contacttitle,address, city, region, postalcode, country, phone,fax)VALUES (PECOF, Pecos Coffee Company,Michael Dunn,Owner, 1900

16、Oak Street, Vancouver, BC,V3F 2K1, Canada,(604) 555-3392,(604) 555-7293) 使用 insert 和select 插入行 USE northwindINSERT customersSELECT substring(firstname, 1, 3) + substring (lastname, 1,2),lastname, firstname, title, address, city,region, postalcode, country, homephone, NULLFROM employees 使用select into

17、 插入 USE northwindSELECT productname AS products,unitprice AS price, (unitprice * 0.1) AS taxINTO #pricetableFROM products 使用top 插入USE northwindSELECT top 10 productname AS products,unitprice AS price, (unitprice * 1.1) AS taxINTO #pricetableFROM products Select * from #pricetable 2. 插入部分数据 USE north

18、windINSERT shippers (companyname)VALUES (Fitch & Mather) SELECT * FROM shippersWHERE companyname = Fitch & Mather3. 使用delete 语句USE northwinddelete shippers WHERE companyname = Fitch & Mather4. 使用truncate table 删除表中所有的行,但保留表的结构和与之相关的对象 TRUNCATE TABLE 语句比 DELETE 语句执行速度快 如果表中有 IDENTITY 列,TRUNCATE TABLE

19、 语句会重新设置原始数据USE northwind TRUNCATE TABLE orders5. 删除基于其他表的行DELETE FROM 表名 | 视图名FROM ,nWHERE 搜索条件 6. 根据表中数据更新use northwind Update products Set unitprice =(unitprice*1.5),列名=新值7. 表操作 Use AdventureWorks Select * from productUse AdventureWorks Select name,productnumber,reorderpoint from product8. 使用字符串比

20、较符通配符描述%0或多个字符串_任何单个的字符在指定区域或集合内的任何单个字符 不在指定区域或集合内的任何单个字符USE northwindSELECT companynameFROM customersWHERE companyname LIKE %Restaurant% 其中包含restrurant9. 使用逻辑运算符优先级 not and orUSE northwind SELECT productid, productname, supplierid, unitprice FROM products WHERE (productname LIKE T% OR productid = 1

21、6) AND (unitprice 16.00) 10. 检索一定范围内的值USE northwindSELECT productname, unitpriceFROM productsWHERE unitprice BETWEEN 10 AND 20 11. 使用值列表作为搜索条件 USE northwindSELECT companyname, countryFROM suppliersWHERE country IN (Japan, Italy) 12. 检索未知值 USE northwindSELECT companyname, faxFROM suppliersWHERE fax I

22、S NULL 13. 对数据进行排序 asc 为正序 desc 为倒序 use northwind select * from employees order by firstname asc(desc) 14. 消除重复行 USE northwindSELECT DISTINCT country FROM suppliers ORDER BY country 15. 使用top NUSE northwindSELECT TOP 5 productname, unitpriceFROM products SELECT TOP 5 PERCENT productname, unitpriceFR

23、OM products 16. 使用聚合函数例子:USE northwindSELECT productid, SUM(quantity) AS QuantitySum from order details GROUP BY productidORDER BY productid 17. 使用 HAVING 子句时,应注意 只在使用 GROUP BY 子句的同时,使用 HAVING子句来限制分组 可以引用任何出现在选择列表中的字段不要联合使用关键字 ALL 和 HAVING 子句。 因为 HAVING 子句会忽略 ALL 关键字,并返回只符合 HAVING 条件的组group by 分组 US

24、E northwindSELECT productid, SUM(quantity) AS QuantitySum from order details GROUP BY productidHAVING SUM(quantity)500ORDER BY SUM(quantity) 18. 连接内部连接是连接类型中最普通的一种,与大多数连接一样,内部连接根据一个或几个相同的字段将记录匹配在一起,但是内部连接仅仅返回那些存在的字段匹配的记录Use northwind select products.*,suppliers.supplierid from products inner join su

25、ppliers on products.supplierid=suppliers.supplieriduse pubsselect a.au_lname+, +a.au_fname as author,t.title from authors ajoin titleauthor ta on a.au_id=ta.au_id join titles t on t.title_id=ta.title_id 外部连接时必需跟上左侧连接还是右侧连接Use pubsselect discounttype,discount,s.stor_name from discounts d left outer j

26、oin stores s on d.stor_id=s.stor_idselect discounttype,discount,s.stor_name from discounts d right join stores s on d.stor_id=s.stor_id 完全连接Use pubsselect discounttype,discount,s.stor_name from discounts d full join stores s on d.stor_id=s.stor_id 交叉连接Use pubsselect discounttype,discount,s.stor_name

27、 from discounts d cross join stores s Union 连接use northwind select companyname ,address,cityfrom customersunionselect companyname,address,cityfrom suppliers IN use northwind select * from customers where customerid in (select distinct customerid from orders) Exists use northwind select * from orders where Exists(select * from customers where customerid=alfki )

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1