mysql 建表 指定路径_用SQL语句建库建表建约束(用SQl语句在指定盘符创建文件夹)...

一 :创建数据库

创建一个数据文件和一个日志文件(MySchool)

create database MySchool

on primary      --默认属于primary主文件组,可省略

(

--数据文件的具体描述

name = 'MySchool_data'    --主数据库文件的逻辑名称

filename = 'D:\project\MySchool_data.mdf', --主数据库文件的物理名称

size = 5MB,     --主数据库文件的初始大小

maxsize = 100MB,     --主数据库文件增长的最大值

filegrowth = 15%     --主数据文件的增长率

)

log on

(

--日志文件的具体描述,各参数含义同上

name = 'MySchool_log',    --主数据库文件的逻辑名称

filename = 'D:\project\MySchool_data.ldf', --主数据库文件的物理名称

size=2MB,     --主数据库文件的初始大小

filegrowth = 1MB    --主数据文件的增长速度

)

go

创建多个数据文件和多个日志文件(employees)

create database employees

on primary

(

--主数据库文件的具体描述

name='employee1',

filename='D:\project\employee1.mdf',

size=10,

filegrowth=10%

),

(

--次要数据库文件的具体描述

name='employee2',

filename='D:\project\employee2.mdf',

size=20,

maxsize=100,

filegrowth=1

)

log on

(

--日志文件1的具体描述

name='employeelog1',

filename='D:\project\employee1_log.ldf',

size=10,

maxsize=50,

filegrowth=1

),

(

--日志文件2的描述

name='employeelog2',

filename='D:\project\empolyee2_log.ldf',

size=10,

maxsize=50,

filegrowth=1

)

go

二:删除数据库

usr master

if exists(select * from sysdatabases where name='....')

drop database ......

三:创建和删除表

use MySchool    --在Myschool中创建表

go

create table Student

(

StudentNo int not null.   --学号,int 类型,不允许为空

LoginPwd nvarchar(50) not null,  --密码 nvarchar类型,不允许为空

StudentName nvarchar(50) not null, --名字,nvarchar类型,步允许为空

Sex bit not null,   --性别,取值0或1

Email nvarchar(20)   --邮箱,可为空

)

go

删除表

use MySchool

go

if exists(select * from sysobjects where naem='Student')

drop table Student

四:创建和删除约束

主键约束(Primary Key Constraint)

非空约束(Not Null)

唯一约束(Unique Constaraint)

检查约束(Check Constaraint)

默认约束(Default Constaraint)

外建约束(Foreign Key Constarint):用于在两表之间建立关系,需要指定引用主表的哪一列

alter table 表名

add constraint 约束名  约束类型  具体的约束说明

例:

--添加主键约束

alter table Student

add constraint PK_StudentNo Primary Key(StudentNo)

--添加唯一约束

alter table student

add constraint UQ_IdentityCard unique(IdentityCard)

--添加默认约束

alter table Student

add constraint DF_Address default('地址不详') for address

--添加检查约束

alter table Student

add constraint CK_BornDate checke(BornDate>='1980-01-01')

--添加外键约束(Result是从表,Student是主表)

alter table Result

add constraint FK_StudentNo

foreign key(StudentNo) references Student(StudentNo)

go

删除约束

alter table 表名

drop constraint 约束名

例:删除学生表中的默认约束

alter tablte Student

drop constraint DF_Address

怎样向已存在数据的表中添加约束

alter table Employee with nocheck ( whit nocheck不向已存在的数据约束)

add constraint

向已存在的数据表中插入一列

alter table 表名

add 列名 数据类型  null

在数据库中用SQL语句创建文件夹(例:在E盘创建一个project文件夹)

exec sp_configure 'show advanced option',1

go

reconfigure

go

exec sp_configure 'xp_cmdshell',1

go

reconfigure

go

exec xp_cmdshell 'mkdir D:\project'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值