数据库建数据库的语法
键数据库的语法
use master
go
-------启动外围配置 创建存放数据库的文件-----------------------
--启用xp_cmdshell
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'xp_cmdshell',1
reconfigure
go
--调用doc命令
exec xp_cmdshell 'mkdir F:\AAA','NO_OUTPUT'
---------------------------------------------------------------------------
-------------创建数据库------------------------------
if exists(select 1 from sys.sysdatabases where [name]='mybassdata')
drop database mybassdata
go
create database mybassdata
on
(
name='aaa_mdf',--主数据库文件的逻辑名
filename='F:\AAA\aaa_mdf.mdf',--主数据文件的物理名
size=3mb,--初始大小
filegrowth=1mb--增长率
)
log on
(
name='aaa_ldf',--日志文件的逻辑名
filename='F:\AAA\aaa_ldf.ldf',--日志文件的物理名
size=1mb,--初始大小
maxsize=15mb,--最大值
filegrowth=1mb--增长率
)
go
------------------------------------------
------建表-------------------
use mybassdata
go
if exists(select 1 from sysobjects where [name]='UserInfo')
drop table UserInfo
go
create table UserInfo--用户信息表
(
userId int not null identity(1,1), --用户编号(主键)
userName varchar(50)not null, --用户名称(不能重复)
userPwd varchar(50)not null --用户密码(密码不能小于6位)
)
go
---------键临时表跟普通的表格一样表名前面加个 “#” 号--------------------------------------
create table #a
(
id int,
name varchar(50)
)
--添加数据
insert into #a(id,name) values(1,'123')
--查询临时表
select * from #a
drop table #a
-------为UserInfo表添加约束------------
alter table UserInfo
add constraint PK_UserInfo_userId primary key(userId),
constraint UQ_UserInfo_userName unique(userName),
constraint CK_UserInfo_userPwd check(len(userPwd)>=6)
go
---------------------------------------------------------------------
if exists(select 1 from sysobjects where [name]='BulletinInfo')
drop table BulletinInfo
go
create table BulletinInfo --公告信息表
(
BulletinId int not null identity(1,1) primary key, --公告编号(主键)
title varchar(100)not null check(len(title)>4), --公告标题
[content] text not null, --公告内容 长度大于100
userId int not null, --发布者(外键)
createTime datetime not null default getdate() --发布时间(默认为系统当前时间)
)
go
--在外面加约束为BulletinInfo表添加约束--------------
alter table BulletinInfo
add constraint FK_BulletinInfo_userId foreign key(userId)references UserInfo(userId)
go
------------------------------------------------------------------------------------------------------------
-----------------在外面加约束为BulletinInfo表添加约束---------------------------------------------
--alter table BulletinInfo
-- add --constraint PK_BulletinInfo_BulletinId primary key(BulletinId),
-- constraint FK_BulletinInfo_userId foreign key(userId)references UserInfo(userId)
---,constraint DF_BulletinInfo_createTime default(getdate()) for createTime
--go
--------
-- -----在外面添加公告的时间默认值----
--ALTER TABLE EMPLOYEES
-- ADD CONSTRAINT EMPLOYEE_HIRE_DATE_DF DEFAULT(getdate()) FOR HIRE_DATE;
------------------------------------------------------------------------------------------------
删除数据库表
SELECT 'drop table '+name+';' FROM sysobjects where name like 'rrs_ykx_goodsDetail_%'