|
简单的创建数据库的 SQL 语句:
use master
go
if exists(select * from sysdatabases where name='Test')
begin
select '该数据库已存在'
drop database Test
end
else
begin
create database Test
on primary
(
name='stuDB_data',
filename='D:\stuDB_data.mdf',
size=5mb,
maxsize=100mb,
filegrowth=15%
)
log on
(
name='stuDB_log',
filename='D:\stuDB_log.ldf',
size=2mb,
maxsize=20mb,
filegrowth=1mb
)
end
接下来是创建数据表的 SQL 语句:
use Test
go
可以先执行一下以上语句。
或者在这里选择数据库。
use Test
go
if exists(select * from sysobjects where name='Student')
begin
select '该表已经存在'
drop table Student
end
else
begin
create table Student
(
S_Id int not null identity(1,1) primary key,
S_StuNo varchar(50) not null,
S_Name varchar(20) not null,
S_Sex varchar(10) not null,
S_Height varchar(10) null,
S_BirthDate varchar(30) null
)
end
alter table Student add constraint
UQ_S_StuNo
unique
(S_StuNo)
alter table Student drop constraint
UQ_S_StuNo
SQL语句创建表变量:
declare @Score table
(
Id int not null,
Name varchar(50) null
)
insert into @Score
select '1','刘邦' union
select '2','项羽'
select * from @Score
SQL语句创建临时表:
create table ##temp
(
Id int not null,
Name varchar(10) null
)
create table #temp
(
Id int not null,
Name varchar(10) null
)
SQL 语句创建表并设置主外键关系:
if exists(select * from sysObjects where name='Course')
begin
select '该表已经存在'
drop table Course
end
else
begin
create table Course
(
Stu_Id int null foreign key(Stu_Id) references Student(S_Id),
C_Id int not null identity(1,1) Primary key,
C_Name varchar(100) not null
)
end
注意:表变量和临时表都不能添加约束,具体的特征和适用场景请参见:
http://www.cnblogs.com/kissdodog/archive/2013/07/03/3169470.html |
|