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

触景伤胃3

触景伤胃3

2016-02-19 21:21

下面是个超简单的SQL Server2005中用语句创建数据库和表教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

  在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',--主数据文件的逻辑名

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  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

 


alter table Team

add

constraint UK_captainId unique(captainId)--唯一约束

go


insert into Team values('第一组',1)

insert into Team values('第二组',2)

insert into Team values('第三组',3)

insert into Team values('第四组',4)

insert into Team values('第五组',5)


insert into Student values('小强','男','1982-6-9','23456789451','来自河北',1)

insert into Student values('小昭','男','1987-6-9','78945678945','山东',4)

insert into Student values('小溪','男','1982-6-9','65987845651','抚顺',3)

insert into Student values('小怜','男','1981-6-9','25487965423','天津',5)

insert into Student(name,sex,birthday,phone,tId) values('李真','男','1984-6-9','25487965423',5)


select * from Team

select * from Student


if exists (select * from sysobjects where name='teacher')

drop table teacher

go

 

create table teacher

(

id int identity (1,1) primary key,

name varchar(20),

address varchar(20)

)


go


insert into teacher values('zhang','hubei')

insert into teacher values('wang','hubei')

insert into teacher values('li','hubei')

insert into teacher values('chen','hunan')

insert into teacher values('zhao','hunan')

insert into teacher values('tian','guangdong')

insert into teacher values('ma','guangdong')

insert into teacher values('chang','tianjin')

insert into teacher values('liang','beijing')


select * from teacher


select count(*),address from teacher group by address having address'hunan'

--按地址分组查询并用having字句筛选出地址不是‘hunan’的

EXEC sp_configure 'xp_cmdshell', 0

GO

-- 还原当前功能(xp_cmdshell)的配置信息为初始状态.

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

RECONFIGURE

GO

EXEC sp_configure 'show advanced options', 0

GO

-- 最后,还原当前高级选项的配置信息为初始状态

RECONFIGURE

GO

展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
SQL Server2005中用语句创建数据库和表

SQL Server2005数据库查询中使用CTE

SQLServer
SQL Server2005数据库查询中使用CTE

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

SQL Server数据库的修复SQL语句

编程语言 网络编程
SQL Server数据库的修复SQL语句

SQL Server 2005数据库升级技巧

SQLServer
SQL Server 2005数据库升级技巧

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

VFP中用SPT访问SQL Server数据库

编程语言 网络编程
VFP中用SPT访问SQL Server数据库

SQL Server 数据库管理常用的SQL和T-SQL语句

SQLServer
SQL Server 数据库管理常用的SQL和T-SQL语句

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

win8系统如何查看共享?

win8系统如何查看共享?

IE7对css选择器的改进

IE7对css选择器的改进
下拉加载更多内容 ↓