--创建前先查询是否存在
if exists (select * from sys.databases where name="Users(数据库名字)")
drop database Users(数据库名字)
go
create database Users(数据库名字)
use Users (数据库名字)
create table usertable(表名字)
(
Ut_Id int not null primary key identity(1,1), --主键,自增长1
Ut_Email varchar(50) null , --邮箱(允许为空)
Ut_Pwd varchar(50) not null, --密码
Ut_Sex bit not null default(1), --性别,1为男0为女
Ut_Qq varchar(50) not null, --QQ
Ut_Necessarytext varchar(50) null, --需要帮助的方面
Ut_Strong_Ct_Id int null, --强项科目,外键(Fk_UserTable_Ut_Strong_Ct_Id(外键名))
Ut_Date datetime not null default(getdate()), --记录时间
)
--科目
Create table CoursTable
(
Ct_Id int not null primary key identity(1,1), --主键,自增长1
Ct_Name varchar(30) not null, --科目名称
Ct_Date datetime not null default(getdate()), --记录添加时间
)
alter table UserTable
add constraint Fk_UserTable_Ut_Strong_Ct_Id foreign key (Ut_Strong_Ct_Id) references CoursTable (Ct_Id)
EXECUTE sp_addextendedproperty N'MS_Description', '会员表(表描述)', N'user', N'dbo', N'table', N'usertable(表名字)', NULL, NULL
EXECUTE sp_addextendedproperty N'MS_Description', '邮箱(字段描述)', N'user', N'dbo', N'table', N'usertable(表名字)', N'column', N'Ut_Email(字段名)'
EXEC sp_updateextendedproperty 'MS_Description','字段描述','user',dbo,'table','usertable(表名字)','column',Ut_Email(字段名)
SELECT *
FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table','usertable(表名字)', 'column', 'Ut_Email(字段名)')
SELECT *
FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table','usertable(表名字)', NULL, NULL)
EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','usertable(表名字)','column',Ut_Email(字段名)
drop table usertable(表名字)
--删除数据库
drop database Users(数据库名字)
ALTER TABLE Admin_Login ADD AdminName nvarchar(50) not null
ALTER TABLE Admin_Login DROP COLUMN AdminName
ALTER TABLE Admin_Login ALTER COLUMN AdminName varchar(50)
Create procedure CreateTable(存储过程名)
(
@tablename nvarchar(50)(参数)
)
as(执行开始)
declare @table nvarchar(1500)(声明变量)
set @table = '
Create table [' + @tablename + 'Article]
(
Article_ID int not null primary key identity(1,1),
Article_Head nvarchar(20) not null,
Article_Text text not null,
Article_ListID int not null,
Article_Time datetime not null default(getdate()),
Article_Delete bit not null default(0),
)
Create table [' + @tablename + 'Photos]
(
Photos_ID int not null primary key identity(1,1),
Photos_Url nvarchar(50) not null,
Photos_Description nvarchar(50) null,
Photos_Time datetime not null default(getdate()),
Photos_Delete bit not null default(0),
)
Create table [' + @tablename + 'Moods]
(
Moods_ID int not null primary key identity(1,1),
Moods_Text nvarchar(50) not null,
Moods_Time datetime not null default(getdate()),
Moods_Delete bit not null default(0),
)
Create table [' + @tablename + 'List]
(
List_ID int not null primary key identity(1,1),
List_Text nvarchar(20) not null,
List_Time datetime not null default(getdate()),
List_Delete bit not null default(0),
)
alter table [' + @tablename + 'Article]
add constraint Fk_Article_Article_ListID_List_List_ID foreign key
(Article_ListID) references [' + @tablename + 'List] (List_ID)
'(设置@table语句为)
exec(@table) (执行改sql语句)