SQL Server 创建数据库基本流程
1.修改当前数据库,用于删除
use master
go
2.如果存在数据库,则进行删除
if exists(select * from sys.databases where name ='test')
drop database test
go
3.创建数据库
create database test
go
4.使用数据库
use test
go
5.删除表(先删除外键表,再删除主键)
if exists (select * from sys.tables where name = 'table')
drop table table
go
6.创建数据表
create tabLe 数据表名称(列1,列2...)
go
1.七大约束:
主键约束:primary key
外键约束:foreign key (references)
唯一值约束:uniQue
默认值约束:defauLt
检查约束:check
非空约束:not nuLL
标识列:identity
2.注意事项:
主外键数据类型必须一致
列于列之间用,间隔,最后一列不加
删除表时,一定要先删除外键表,再删除主键
3.常用函数:
getdate():获取当前日期和时间
7.插入数据:推荐单行插入,尤其是主键表,必须使用单行插入
insert into table values(...)
go
1.空数据插入null
2.默认值插入default
8.执行查询:单表查询及联合查询