Create database/Create table 示例(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:\data\Study_data.mdf',-- 主数据文件的物理名
       size=10MB,-- 初始大小
       filegrowth=10%    -- 增长率
    )
log on
    (
       name='Study_log',-- 日志文件的逻辑名
       fileName='D:\data\Study_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’
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值