sqlserver中创建数据库和表的语法下⾯是sql server中创建数据库,创建数据表以及添加约束的sql语句:
use master
--创建数据库
if exists (select * from sysdatabases where name = 'jobtest')
drop database jobtest
create database jobtest
on
(
name='jobtest_data',
filename = 'D:\DB\jobtest_data.mdf',
size = 10MB,
filegrowth = 10%
)
log on
(
name = 'jobtest_log',
filename = 'D:\DB\jobtest_log.ldf',
size = 10MB,
maxsize = 20MB,
filegrowth = 1MB
)
go
-
-创建student表
if exists (select * from sysobjects where name = 'student')
drop table student
create table student
(
id int not null,
name varchar(50) not null,sql数据库创建表步骤
age int not null
)
go
--创建grade表
if exists (select * from sysobjects where name = 'grade')
drop table grade
create table grade
(
id int not null,
gname varchar(50) not null
)
--添加约束
--主键
alter table student
add constraint pksid primary key (id)
-
-唯⼀约束
alter table student
add constraint ukid unique(id)
--默认约束
alter table student
add constraint df_name default('张三') for name
--检查约束
alter table student
add constraint ck_age check (age>0 and age<100)
--必须给另⼀个表添加主键
alter table grade
add constraint pkid primary key (id)
--外键约束
alter table student
add constraint fkgid foreign key (id) references grade (id)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论