1、
2、
3、
4、
在常规中做如下修改:
然后在左边选择页中的用户映射中选中QQ
下面不做修改,点击确定。登录名中就会多出来一个QQ,同时QQ数据库---安全性---用户列表里也会多一个QQ,但是属性中无法修改用户名和登录名,如果用QQ登陆,就会无法对别的数据库进行操作,也无法对QQ数据库创建表,因为没有权限,所以就需要删除数据库中QQ ---安全性---用户中的QQ,重新创建一个用户如bbb,用户名设为bbb,登录名设为QQ,最下面的角色成员选择
点击确定。然后断开连接,在文件处点击连接对象资源管理器,选择登录名为QQ和相应密码登录,然后就可以对数据库QQ进行创建表,对别的数据库不能进行操作。
5、
createdatabaseMyDB
onprimary//默认组名
(
name=mydb_dat,
filename='F:\data\mydb_dat.mdf',
size=5mb,
maxsize=20mb,
filegrowth=10%
)
,
filegroupdbfg//次要文件的组名
(
name=dbfg_dat1,
filename='E:\data\mydb_dat1.ndf',
size=5mb,
maxsize=20mb,
filegrowth=5%
),
(
name=dbfg_dat2,
filename='D:\data\mydb_dat2.ndf',
size=5mb,
maxsize=10mb,
filegrowth=1mb
)
logon//日志文件
(
name=mydb_log,
filename='F:\data\mydb_log.ldf',
size=5mb,
maxsize=20mb,
filegrowth=10%
)
6、
7、
8、
execsp_dboption'yourDB','single',true//锁定
execsp_renamedb'yourDB','myDB'//修改
execsp_dboption'myDB','single',false //保存
9、
use master
alter database myDB
modify file
(
Name=mydb_dat,
Size=6mb,
Filegrowth=1%
10、
usemaster
alterdatabasemyDB
addfile
(
filename='F:\data\mydb_dat1.ndf',
name=mydb_dat1,
size=6mb,
filegrowth=1%
)
11、
12、
13、
Exec sp_attach_dbmyDB, ’f:\data\myDB_dat.mdf’
若附加的文件超过了16个,可使用create
14、
15、
16、
创建:
删除:
如:execsp_addtypenameType,'nvarchar(20)','not null'
execsp_droptypenameType
17、
18、
createtable#MyTempTable(colaintprimarykey,idint)//创建临时表
insertinto#MyTempTablevalues(1,3)//插入相关数据
insertinto#MyTempTablevalues(2,6)
select*from#MyTempTable//查询该表
查询结果为:
createtable##MyTempTable2(idintprimarykey,namechar(20))
//创建临时全局表
insertinto##MyTempTable2values(1,'张三') //插入相关数据
select*from##MyTempTable2//查询该表
19、
useTestDB//指定数据库名
createtableTestTable//在指定数据库中创建表
(
dateasgetdate(),//自动获得日期时间
idint,
usenameasuser_name()//自动获得该表的用户名,如(dbo)
)
20、
useTestDB
declare@ttable
(
col1int,
col2varchar(10)
)
insertinto@tvalues(1,'很好')
select*from@t
21、
useTestDB
altertableTestTable
addphoneintnull,
emailchar(20)null
22、
useTestDB
altertableTestTable
altercolumnemailvarchar(20)null
23、
Exec sp_rename ‘架构名.表名.旧字段名‘,新字段名,’column
24、
altertablecustoms
dropcolumncustomsAge
约束
25、
26、
27、
28、
createtablecustoms
(
customerNointidentitynot
customerNamevarchar(30)notnull
)
29、
altertablecustoms
addconstraintPK_customerNo//添加约束
Primarykey(customerNo)//选定主键
30、
createtableorders
(
orderIDintnotnullprimary
customerNointnotnullfore
)
31、
altertableorders
addconstraintFK_EmployeeCreatesOredr//添加外键约束
foreignkey(customerno)referencescustoms(customerno)//参照customs表中的no主键来创建本表的外键
32、
createtableorderDatails
(
orderidintnotnull,
partnovarchar(10)notnull,
constraintPKOrderDetails
constraintFKOrdercontrai
onupdatenoaction
ondeletecascade
)
33、
创建表时创建唯一约束:
createtableshippers
(
shipperidintidentitynotn
phoneno1varchar(14)notnullunique,
phonenovarchar(14)notnull
)
在已存在的表中创建唯一约束:
altertableshippers
addconstraintAK_ShippersPhoneNounique(phoneno)
34、
altertablecustoms
addconstraintcn_customerdateinsystem
check
(DatelnSystem<=GetDate())
删除约束:
altertablecustoms
dropconstraintcn_customerdateinsystem
35、
创建默认值
createtableshippers
(
shipperidintidentitynotn
dateinsystemsmalldatetim
)
在已存在的表上加默认值
Altertablecustomers
Addconstraintcn_customerdefaultdateinsys
Defaultgetdate()fordateinsystem
对表进行增删改查
36、
37、
insertintostudents(stuno,stuname,sex,age)
values(6,'张三','男',20)
38、
以使用top限制插入的行:
Selecttop 1customerNoasno,customerNameasname
//插入前一行
into#custom
fromcustoms//从customs表中的数据中创建临时表
select*from#custom//查询临时表
39、
插入:Insertstudent(id,name)
values(6,'张三')
查询:select*fromstudent
whereid=6 andname='张三'
40、
如果要删除某一行可添加whete语句,如果删除前几行可使用top语句
41、
使用where指出哪些行要更新
用set指定新值
useNorthwind
updateProducts
setUnitPrice=(UnitPrice*1.5)
42、
varchar、nvarchar和datetime,通配符的种类:
模糊查询companyname列包含restaurant的数据:
selectcompanynamefromCus
whereCompanyNamelike'%restaurant%'
43、
selectproductid,productname,supplierid,unitprice
fromProducts
where (ProductNamelike'T%'orProductID=16)
//产品名称以T开头或产品ID号为16的
and(UnitPrice>16.00)
44、
selectproductname,unitprice
fromProducts
whereUnitPricebetween 10 and 20
45、
值,使用not in搜索条件来返回值不在指定列表中的行
selectcompanyname,countryfromSuppliers
whereCountryin('Japan','Italy')
46、
selectcompanyname,fax
fromSuppliers
whereFaxisnull
47、
selectEmployeeID,TitlefromEmployees
whereEmployeeID=6
orderbyFirstNamedesc
48、
selectdistinctCountryfromSuppliers
orderbyCountry
49、
selectdistincttop 5 Country//取前五行不重复的值
fromSuppliers
orderbyCountry
50、
selectproductid,SUM(quantity)asquantitySum,
AVG(Quantity)asqualityAvg//求数量总和和平均值
from[Order Details]
whereProductID=6
groupbyProductID
orderbyProductID
51、
如果同时出现group by、order by和where,where应放在最前面,order by放在最后面;
52、
selectproductid,SUM(quantity)asquantitysum
from[Order Details]
groupbyProductID
havingSUM(Quantity)>500
orderbySUM(Quantity)
连接
53、
selectProducts.*,Suppliers.SupplierID
fromProductsinnerjoinSuppliers
onProducts.ProductID=Suppliers.SupplierID
54、
左连接:selectProducts.*,Suppliers.SupplierID
fromProductsleftjoinSupp
onProducts.ProductID=Suppliers.SupplierID
右连接:selectProducts.*,Suppliers.SupplierID
fromProductsrightjoinSup
onProducts.ProductID=Suppliers.SupplierID
55、
selectdiscounttype,discount,s.stor_name
fromdiscountsd
fulljoinstoressond.stor_id=s.stor_id
56、
selectdiscounttype,discount,s.stor_name
fromdiscountsd
crossjoinstoress
关键字查询
57、
selectcompanyname,address,cityfromCustomers
union
selectcompanyname,address,cityfromSuppliers
58、
select*fromCustomerswhereCustom
(selectdistinctCustomerID
59、
select*fromOrderswhere
exists
(select*fromCustomerswhereCustom
60、
selectorderid,freightfromOrders
whereFreight<any
(selectSUM(unitprice)from[Order Details]groupbyOrderID)
61、
Select'The Customer has an Order numbered '+cast
(orderidasvarchar)
fromorderswherecustomeri
视图
62、
createviewv_clastu
as
selectorders.orderID,customs.customerNo,customs.customerName
fromordersinnerjoincusto
onorders.customerNo=customs.customerNo
63、
自定义函数
64、
标量函数:返回一个标量(单值)结果
createfunctionfn_DateFormat
(@indatedatetime,@separatorchar(1))
returnsnchar(20)as
begin
return
convert(nvarchar(20),datepart(yy,@indate))
+@separator
+convert(nvarchar(20),datepart(mm,@indate))
+@separator
+convert(nvarchar(20),datepart(dd,@indate))
end
selectdbo.fn_DateFormat(GETDATE(),':')//查询函数
查询的返回结果是:
创建架构绑定函数:函数创建时使用SchemaBinding,创建后,所引用的数据库对象不能被更改。
创建、更改或删除用户定义函数,必须具有create function权限;
其他用户使用函数,则必须具有execute权限;
若函数是架构绑定的,则创建者必须具有函数所引用的表、视图和函数上的reference权限;
更改函数:alter function
删除函数:drop function
表值函数:返回table数据类型,函数体内允许有:赋值语句、流控制语句、declare语句、select语句
Begin和end分隔了函数体
Returns子句指定table作为返回的数据类型
Returns子句定义了返回表的名字和格式
createfunctionfn_employees(@lengthvarchar(9))
returns@fn_employeestable//返回table
(employeeIDintprimarykeyn
[employee name]nvarchar(61)notnull)
as
begin
if@length='shortname'
insert@fn_employeesselectemployeeI
elseif@length='longname'
insert@fn_employeesselectemployeeI
(firstname+''+lastname)fromemployees
return
end
select*fromdbo.fn_employees('longname')//调用函数
内置函数:系统提供,返回标量数据类型或table数据类型
存储过程
65、
createprocGetPassWord
@callnchar(10),
@passwordnchar(10)output
as
select@password=passwordfromV_users
wherecall=@call
66、
declare@passwordnchar(10)
execGetPassWord'妈妈',@passwordoutput
print@password
67、
createprocGetuser
as
select*fromuseres
select*fromfamilyUser