SQL语句(创建书库,创建建表,写入注释,获取注释)

--创建前先查询是否存在

if exists (select * from sys.databases where name="Users(数据库名字)")

  drop database Users(数据库名字)

  go

--创建数据库

create database Users(数据库名字)

--使用该数据库

use database 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

--为字段Ut_Email添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '邮箱(字段描述)', N'user', N'dbo', N'table', N'usertable(表名字)', N'column', N'Ut_Email(字段名)'

--更新表中列Ut_Email的描述属性:
EXEC sp_updateextendedproperty 'MS_Description','字段描述','user',dbo,'table','usertable(表名字)','column',Ut_Email(字段名)

--显示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)
--删除表中列Ut_Email的描述属性

EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','usertable(表名字)','column',Ut_Email(字段名)

--删除表

drop table usertable(表名字)

--删除数据库

drop database Users(数据库名字)

 

--新建存储过程(用存储过程建表)

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语句)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值