SQL基本语句大全

更新时间:2023-03-09 08:58:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第一章 数据库设计

一)设计数据库的步骤

1. 需求分析阶段

a.收集实体 b.表示对象

c.表示每个对象需要储存的详细信息 d.表示对象之间的关系 2. 概要设计阶段

绘制E-R图 3. 详细设计阶段

把E-R图转换为表 4. 实体关系模型

a. 属性-椭圆 b. 实体-长方形 c. 菱形-关系

二)如何将E-R图装换为表

1.将个实体转化为对应的表,将个属性转化为个表对应的列 2.标识每个表的主键列

3.我们还需要在表之间体现实体之间的映射关系

三)数据范式

1.第一范式(NF,Normal Formate)确保每列的原子性

2.第二范式(2NF,Normal Formate)确保表中的每列都和主键相关 3.第三范式(3NF,Normal Formate)确保表中的每列都和主键直接相关

四)创建数据库的语法

use master

if exists(select * from sysdatabases where name='数据库名') drop database 数据库名 create database数据库名 on( name='stu', filename='d:\\stu3.mdf' )

log on( name='stu_log', filename='d:\\stu3.ldf' ) Go

第二章 数据库的实现

一)创建表语法

use 数据库

if exists(select * from sysobjects where name='studentinfo')

drop table studentinfo create table studentinfo( sid int identity(1,1) primary key, sname varchar(20) not null, sage int not null default(18), semail varchar(50) check(semail like '%@%'), saddress varchar(50) ) go

use 数据库

if exists(select * from sysobjects where name='score') drop table score create table score( stuid int foreign key references studentinfo(sid), writtenexam int default(0) not null, labexam int default(0) not null ) Go

二)添加约束

a.添加主键约束

alter table 表名

add constraint pk_主键列名 primary key(列名) b.添加唯一约束(如身份证号) alter table 表名

add constraint un_列名 uniqun(列名) c.添加默认约束

alter table 表名

add constraint df_列名 default(‘默认值’) for 列名 d.添加检查约束 alter table 表名

add constraint ck_列名 check(列名 between 值1 and 值2) e.添加外建约束

alter table 表名

add constraint fk_列名 forign key(列名) references 主表(列名)

三)删除约束

alter table 表名

drop constraint 约束名 四)创建sql 登陆账号 1.创建登陆账号

Exec sp_addlogin ‘账号名’,‘密码’ 2.创建数据库登陆账号

Exec sp_grantdbaccess ‘账号名’,‘数据库用户’ 3.给数据库用户授权

Grant 权限[ on 表名] on 数据库用户

第三章 T-SQL编程

一)局部变量

1.Declare @局部变量名 数据类型

局部变量的名称必须以标记@作为前缀 2.局部变量赋值

a. set @局部变量名=value

b. select @局部变量名=value(一般用于从表中查询数据,然后在赋值。查询结果不能多于一条)

二)全局变量

全局变量的名称必须以标记@@作为前缀

常用的两个全局变量:@@error 返回最后一个错误的错误号 @@identity 返回最后一个标识列

三)输出语句

1.print 局部变量或字符串(输出的是消息,只能打印字符串) 2.select 局部变量 AS 自定义列名 四)逻辑控制语句 1.if-else语句 If (条件)

Begin(如果if语句默认只带一条语句,可以省略) 语句或语句块 end

Else

Begin(如果if语句默认只带一条语句,可以省略) 语句或语句块 end 2.循环

While(条件)

Begin(如果if语句默认只带一条语句,可以省略) 语句或语句块 end

3.case 多分支语句 Case

Where 条件1 then 结果1 Where 条件2 then 结果2 Where 条件3 then 结果3

[else 其他结果]

第四章 高级查询

一)简单的子查询

1.采取运算符子查询

例如:查询班级中比张三年龄大一岁的同学信息

select sname from studentinfo where sage>(select sage from studentinfo where sname='张三')

先执行子查询后执行父查询

使用“=”,“>“??等比较运算符时,要求子查询只能返回一条或空的记录 当子查询在=,<,>,!<,<=,>=之后 不允许子查询返回多天记录 2.采取in\\not in进行子查询

例如:查询班级中参加了考试的同学信息

select * from studentinfo where sid in (select stuid from score ) 例如:查询班级中没有参加考试的同学信息

select * from studentinfo where sid not in (select stuid from score ) 3.exists 和 not exists 子查询

可以作为where 的子查询,一般用于if 语句的存在检测 If exists (子查询)语句

第五章 事务、索引、和视图

一)事务

1.事务的ASID属性 a. Atomicity(原子性) b. Consistency(一致性) c. Isolation(隔离性) d. Durability(持久性) 2.创建事务(显示事务) a.开始事务 begin transaction b.提交事务commit transaction c.回滚事务 rollback transaction 二)索引 1.索引分类

主键索引 特殊的唯一索引 唯一索引 不允许重复

聚集索引 表中只允许有一个 非聚集索引 2.创建索引

If exists(select * from sysoindexes where name=’要创建的名’) Drop index 表名.要创建的名

Create [unique](唯一索引),[clustered](聚集索引),[nonclustered](非聚集索引) On table_name (column_name[,??column_name]??) [With

Fillfactor=X(可省略) ]

Select * from 表名 with(index=名) 三)视图 创建视图

If exists(select * from sysobjects where name=’要创建的名’) Drop view 要创建的名 Create view view_name

As

Sql语句

第六章 存储过程

一)存储过程

用以管理sql和显示有关的数据库和用户的信息 1.常用的系统存储过程名称一般用sp_开头,xp_开头保存在master数据库中 exec sp_addlogin 创建SQL登陆账号 exec sp_grantdbaccess 创建数据登陆账号 exec sp_databases 查看数据库的名称

exec sp_password '','123' 修改当前登录的用户密码 exec sp_renamedb 'studentdb','student' 修改数据库名 exec xp_cmdshell 'md d:\\1234' 用DOC命令创建文件夹 2.用户自定义存储过程 a.不带参

If exists(select * from sysobjects where name=’proc_要创建的名’) Drop proc 要创建的名

Create proc[edvre] 存储过程名

[ {@参数一 数据类型 }][=默认值][output] (中括号中的语句可以省略) 有输出参数是用【output】

???????????????? As

加 Sql语句 b.带参

Create procp[edvre] 存储过程名

@参数一 数据类型 [=默认值][output] ???????????????? As

Sql语句

3.处理错误信息

Raiseeeor (‘提示的话 ’,12,1)12是错误的等级,1是状态号 4

调用带参数的存储过程的exec 先定义变量 Declare @a int

Exec 存储过程名 @a 后面加输入参数

本文来源:https://www.bwwdw.com/article/a3br.html

Top