SQL语句(创建书库,创建建表,写入注释,获取注释)
2013-12-10 19:18
423 查看
--创建前先查询是否存在
if exists (select * from sys.databases where name="Users(数据库名字)")
drop database Users(数据库名字)
go
--创建数据库
--使用该数据库
--创建表
--创建外键
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字段的描述属性
--显示表的描述属性
--删除表中列Ut_Email的描述属性
--删除表
--删除数据库drop database Users(数据库名字)
--添加列
--删除列
--修改列属性
--新建存储过程(用存储过程建表)
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
--为字段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(数据库名字)
--添加列
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语句)
相关文章推荐
- SQL语句(创建书库,创建建表,写入注释,获取注释)
- 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句
- 获取oracle中某一用户某一表空间下的表、列注释说明的SQL查询语句
- sql语句创建表的时候加表注释和列注释
- 笔记:mysql查看表的结构及注释、创建表sql语句方法。
- oracle获取表的字段名称、字段类型、长度、注释、主键的sql语句
- 2015.7.30 第十五课 sql(新建数据库、创建表、注释、查询语句、新增、更新、删除、联合查询)
- nullnull使用PL/SQL获取创建用户的语句
- 打开powerDesigner时,创建table对应的自动生成sql语句没有注释
- 获取创建控制文件的sql语句
- 加载驱动+建立连接+创建sql语句+执行并获取结果+释放连接 增删改查
- 突破MySQL视图限制:获取创建视图的SQL语句
- 使用SQL语句获取数据库中随机N个记录
- MySQL数据库执行sql语句创建数据库和表提示The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have
- oracle spatial 创建空间字段索引 创建字段索引 完善SQL语句实例二
- ^全^ 获取SQL SERVER2000/2005、MySql、Oracle元数据的SQL语句(1)
- Sql语句不能识别Go的解决办法(动态创建表的触发器)
- sql语句自动创建表
- 用sql语句创建一个与表A字段相同的表B
- SQL语句分组获取记录的第一条数据的方法