繁体中文
设为首页
加入收藏
当前位置:技术首页 >> 数据库 >> Sql server >> 在SQL Server2005中用语句创建数据库和表

在SQL Server2005中用语句创建数据库和表

2008-04-07 23:14:06  作者:IT动力源  来源:IT动力源收集整理  浏览次数:0  文字大小:【】【】【

在SQL Server2005中用语句创建数据库和表:

具体示例如下:

use master

go

if exists (select * from sysdatabases where name="Study")

--判断Study数据库是否存在,如果是就进行删除

drop database Study

go

EXEC sp_configure "show advanced options", 1

GO

-- 更新当前高级选项的配置信息

RECONFIGURE

GO

EXEC sp_configure "xp_cmdshell", 1

GO

-- 更新当前功能(xp_cmdshell)的配置信息。

RECONFIGURE

GO

exec xp_cmdshell "mkdir D:data", NO_OUTPUT

--利用xp_cmdshell 命令创建文件夹,此存储过程的第一个参数为要执行的有效dos命令,第二个参数为是否输出返回信息。

go

create database Study--创建数据库

on primary

(

name="Study_data",--主数据文件的逻辑名

fileName="D:dataStudy_data.mdf",--主数据文件的物理名

size=10MB,--初始大小

 filegrowth=10% --增长率)

log on

(

name="Study_log",--日志文件的逻辑名

fileName="D:dataStudy_data.ldf",--日志文件的物理名

size=1MB,

maxsize=20MB,--最大大小

filegrowth=10%

)

go

use Study

go

if exists (select * from sysobjects where name="Student")--判断是否存在此表

drop table Student

go

create table Student

(

id int identity(1,1) primary key,--id自动编号,并设为主键

[name] varchar(20) not null,

sex char(2) not null,

birthday datetime not null,

phone char(11) not null,

remark text,

tId int not null,

age as datediff(yyyy,birthday,getdate())--计算列。

)

go

if exists (select * from sysobjects where name="Team")

drop table Team

go

create table Team

(

id int identity(1,1) primary key,

tName varchar(20) not null,

captainId int

)

go

alter table Student

add

constraint CH_sex check(sex in ("男","女")),--检查约束,性别必须是男或女

constraint CH_birthday check(birthday between "1950-01-01" and "1988-12-31"),

constraint CH_phone check(len(phone)=11),

constraint FK_tId foreign key(tId) references Team(id),--外键约束,引用Team表的主键

constraint DF_remark default("请在这里填写备注") for remark--默认约束,

go


点击收藏到

责任编辑:hefei

本文引用地址: http://tech.itzero.com/2008/0407/35171.html 请粘贴到你的QQ/MSN上推荐给你的好友

相关文章
Windows Server 2008曝设计安全缺陷
Windows Server 2008曝严重漏洞 影响Vista系统安全
设置sql server实现互联网数据库安全复制
在oracle中利用角色增强应用系统安全性
修改SQL Server 2000身份验证模式和系统管理员
sql server 7.0安全问题
sql server 7.0安全模式中最重要的改变
数据库快照,自定义函数与计算列的概念
讲解SQL Server 2005数据库的同义词Bug
实例讲解如何从结果集中获得随机结果
详细讲解大型数据库的设计原则与开发技巧
 

最新文章

更多

· 数据库快照,自定义函数与...
· 讲解SQL Server 2005数据...
· 实例讲解如何从结果集中...
· 详细讲解大型数据库的设...
· MS SQL Oracle MySQL查出...
· SQL Server 2000/2005下...
· 详细介绍分级汇总实现的...
· 如何用SQL写出当M*N时的...
· 在SQL Server中获得不包...
· SQL Server 2008关系数据...

热点文章

更多

· SQL2000里的数据类型
· SQL Server导出导入数据方法
· SQL SERVER日志清除的两...
· 实战MSSQL 2000数据库之...
· 向SQLServer数据库读写i...
· SQL转ACCESS解决自动编号...
· SQL Server数据库开发的...
· Asp备份与恢复SQL Server
· SQL Server快速参考
· ASP中调用存储过程、语法...

其它推荐