Microsoft
1、Master 数据库
2、Tempdb 数据库
3、Model 数据库
4、Msdb 数据库
sql语句
1、
Select * into <新表> from <旧表>
2、
Select * into <新表> from <旧表>
3、
Insert
Select stuName , stuAge , stuSex , stuAddress
From Student
Where stuAge > 30
注意:Student表中有标识列时不能用*号代替列
4、
Select
--用delete和truncate删除数据的区别
delete from student
truncate table student
--删除表结构
drop table student
--删除数据库
drop database student
--注意:只有该条没有外键时,才能删除数据。
--聚合函数
count() --计数
avg() --求平均值
sum() --求和
max()
min()
--注意:avg()和count()不计算值
--判断数据库是否存在
if exists(select * from sysdatabases where name='数据库名')
--判断表是否存在
if exists(select * from sysobjects where name='表明')
--修改姓名列
alter table student
alter column stuName varchar(100) not null
go
--添加主键约束
alter table student
add constraint PK_student primary key(列名)
go
--添加唯一约束;允许有null值
alter table student
add constraint UQ_stuName unique(列名)
go
--添加默认值
alter table student
add constraint DF_stuAge_student default(18) for <列名>
go
--添加检查约束
alter table student
add constraint CK_stuSex check(stuSex like '[男女]')
go
--添加外键约束
alter table 外键表
add constraint FK_stuAge foreign key (外键列)
references 主键表(主键列)
go
--删除约束
alter table student
drop constraint 约束名
go
--连接
--内连接
select 列名 from 表名 where 公共字段
--或
select <列名> from <表名> insert join <表名> on <公共字段>
--外连接
--1、左外连接查询
select <列名> from <表名> left [outer] join <右表> on <公共字段>
--2、右外连接查询
select <列名> from <右表> right [outer] join <左表> on <公共字段>
--创建视图
create view view_name
as
go
--打开视图
select * from 视图名
--创建事务
begin transaction --开始事务
commit transaction --提交事务
rollback transaction --回滚事务
go
--打开隐式事务
--与显示打开事务的区别(一个事务的结束作为下一个事务的开始)
--查询临时表是否存在
if OBJECT_ID('TEMPDB.abo.#TEMPDB') is not null
drop table #TEMPDB
go
--创建函数
create function SubStuName
(@stuName varchar(20)) --姓名
returns varchar(10)
as
declare @m = varchar(20)
set @m = @stuName
set @m = LEFT(@stuName,2)
return @m
go
--调用函数
select abo.SubStuName(stuName) from <表>