SQL语法
1.添加数据:insert [into]表名 (字段1, 字段2,??)values (值1,值2) 示例:insert into stuInfo (stuName,stuNo,stuAge) values (‘张三’,’s25301’,22) 2.修改数据:update 表名 set字段1=值1,字段2=值2,??where (条件) 示例:update stuInfo set stuAge=25 where stuName=’张三’ 3.查询数据:select字段1 , 字段2 ,?? from 表名 where (条件) order by 字段名 示例:select stuName , stuNo from stuInfo where stuAge < 25 order by stuNo 4.删除数据:delete from 表名 where (条件) 示例:delete from stuInfo where stuAge <20 5.创建数据库:create database 数据库名
on [primary] (
<数据文件参数> [,?n] [<文件组参数>] ) [log on]
(
{<日志文件参数> [,?n]}
)
文件的具体参数的语法如下: ([name=逻辑文件名,] Filename=物理文件名
[,size=大小] [,maxsize={最大容量|unlimited}] [,filegrowth=增长量] ) [,??n]
示例:
create database stuDB
on primary --默认就属于primary主文件组,可省略 ( /*--数据文件的具体描述--*/ name=’stuDB_data’, --主数据文件的逻辑名称
filename=’D:\\project\\stuDB_data.mdf’, --主数据文件的物理名称 size=5mb, --主数据文件的初始大小 maxsize=100mb, --主数据文件增长的最大值 filegrowth=15% --主数据文件的增长率 )
log on
(
/*--日志文件的具体描述,个参数含义同上--*/ Name=’stuDB_log’,
Filename=’d:\\project\\stuDB_log.ldf’, Size=2mb,
Filegrowth=1mb
) Go
6.删除数据库:drop database 数据库名 示例:drop database stuDB 7.创建表:create table 表名
(
字段1 数据类型 列的特征, 字段2 数据类型 列的特征, ?
)
示例:use stuDB `
Go
create table stuInfo /*-创建学院信息表-*/
( stuName varchar(20) not null, --学院姓名,非空(必填)
stuNO char(6) not null, --学号,非空(必填)
stuAge int not null, --年龄,int类型不用指定大小,默认4个字节 stuID numeric(18,0) --身份证号,numeric (18,0)代表18位数字,小数位数为0 stuSeat smallint identity (1,1), --座位号,自动编号(标识列),从1开始递增 stuAddress text –地址,允许为空,即可选输入
) Go
8.删除表:drop table 表名 示例:drop table stuInfo
9.添加约束:alter table 表名 add constraint 约束名 约束类型 具体的约束说明
示例:---添加主键约束(将stuNo作为主键)
Alter table stuInfo add constraint pk_satuNO primary key (stuNo)
---添加唯一约束(身份证号唯一,因为每个人的身份证号全国唯一)
Alter table stuInfo add constraint uq_stuID unique (stuID) ---添加默认约束(如果地址不填,默认为“地址不详”)
Alter table stuInfo add constraint df_stuaddress default (‘地址不详’) from stuAddress ---添加检查约束,要求年龄只能在15~40岁之间
Alter table stuInfo add constraint ck_stuAge check(stuAge between 15 and 40)
---添加外键约束(主表stuInfo 和从表stuMarks 建立关系,关键字段为stuNo) Alter table stuMarks add constraint fk_stuNO foreign key(stuNO) references stuInfo(stuNO)
Go
10.删除约束:alter table 表名 drop constraint 约束名 示例:alter table stuInfo drop constraint df_stuAddress 11.创建登陆账号:/*--添加Windows 登陆账号--*/
Exec sp_grantlogin ‘windows 域名\\域账户’ /*--添加SQL登陆账户*/ Exec sp_addlogin ‘账户名’,‘密码’
示例:/*--添加Windows 登陆账号--*/
Exec sp_grantlogin ‘jbtraining\\s26301’ /*--添加SQL登陆账户*/ Exec sp_addlogin ‘zhangsan’,‘1234’
12.创建数据库用户:exec sp_grantdbaccess ‘登陆账号’,‘数据库用户’
示例:/*--在stuDB数据库中添加两个用户--*/
use stuDB
Go
Exec sp_grantdbaccess ‘jbtraint\\s26301’,‘s26301DBUser’ --s26301DBUser为数据库用户名
Exec sp_grantdbaccess ‘张三’,‘zhangsanDBUser’
13.给数据库用户授权:grant 权限 [on 表名] to 数据库用户
示例:use stuDB Go /*--为zhangsandBUser分配对表stuInfo的select、insert、update权限--*/
Grant select , insert , update on stuInfo to zhangsanDBUser /*--为s26301DBUser分配创建表的权限--*/
Grant create table to s26301DBUser 14.局部变量:declare @变量名 数据类型 示例:declare @name varchar(8) 15.变量的赋值:set @name=value
或
Select @ name=varlue 示例:set @name=‘哈哈’ 或
Select @ name=sname from stuInfo where id =1 16.全局变量:@@变量名
示例:@@error
17.输出语句:print 局部变量或字符串 Select 局部变量 as 自定义列名
示例:print ‘服务器的名称:’+ @@servername Select @@servername as ‘服务器名称’
18.if-else条件语句:if(条件) 语句或语句块
Else 语句或语句块 如果有多条语句 if(条件) begin 语句1 语句2 End Else
?
?
示例:
Delcare @myavg float
Select @myavg=avg(writtenexam) from stuMarks Print ‘本班平均分’+convert(varchar(5),@myavg) If(@myavg>70)
Begin Print ‘本班笔试成绩优秀,前三名的成绩为’ Select top 3 * from stuMarks order by writtenexam desc End
Else
Begin Print ‘本班笔试成绩较差,后三名的成绩为’ Select top 3 * from stuMarks order by writtenExam
End
19.While循环语句:while (条件)
语句或语句块
[break]
示例:insert into stuMarks(examNo,stuNo,writtenExam,LabExam) --插入测试数据
Values(‘s271819’,’s25318’,56,48) Select * from stuMarks Delcare @n int
While(1=1) --条件永远成立
Begin Select @n=count(*) from stuMarks where writtenExam < 60 --统计不及格人
数
If(@n>0)
Update stuMarks set writtenExam=writtenExam+2 --没人加2分 Else Break --退出循环
End
Print ‘加分后的成绩如下:’
Select * from stuMarks
20.case多分支语句:case
When 条件1 then 结果1 When 条件2 then 结果2 [else 其他结果]
End
示例:select * from stuMarks --原始成绩
Print ‘ABCDE五级显示成绩如下:’ Select stuNo,成绩=case
When writtenExam<60 then ‘E’
When writtenExam between 60 and 69 then ‘D’ When writtenExam between 70 and 79 then ‘C’
When writtenExam between 80 and 89 then ‘B’
Else ‘A’ End from stuMarks
21.子查询:select ? from 表1 where 字段1 (比较符号) (子查询)
示例:select * from stuInfo where stuAge > (select stuAge from stuInfo where stuName =’
李斯文’)
22.in 和not in 子查询:select ? from 表1 where 字段1 in(或not in) (子查询)
示例:select stuName from stuInfo where stuNO in (select stuNO from stuMarks where writtenExam=60)
23.exists 和 not exists 子查询:if exists(子查询)
语句
示例:if exists (select * from stuMarks where writtenexam > 80)
Begin Print ‘本班有人笔试成绩高于80分,每人只加2分,,加分后的成绩为:’
Update stuMarks set writtenExam=writtenExam+2 Select * from stuMarks End Else
Begin Print ‘本班无人笔试成绩高于80分,每人可以加分5分,加分后的成绩为:’
Update stuMarks set writtenExam =writtenExam+5 Select * from stuMarks End Go
24.事务:开始事务:begin transaction
提交事务:commit transaction
回滚(撤销)事务:rollback transaction 示例:use stuDB
Go
Set nocount on
Print ‘查看转账事务前的余额’ Select * from bank Go
Begin transaction
Declare @errorSum int
Selt @errorSum=0
Update bank set currentMoney=currentMoney-1000 where customerName=’张三’ Set @errorSum=@errorSum + @@error
Update bank set currentMoney=currentMoney+1000 where customerName=’李四’ Set @errorSum=@errorSum+@@error Print 查看转账事务过程中的余额‘ Select * from bank If @errorSum<>0