首页 > 技术文章 > 数据库的实现法

weiguangyi 原文

数据库的实现

1.创建一个名称为S2222的数据库

--第一步执行sp configure 启用xp_cmddshell

exec sp_configure 'show advanced options',1

go

reconfigure

go

--https://howsecureismypassword.net/

exec sp_configure 'xp_cmdshell',1

go

reconfigure

go

--01.Server版操作系统

--第二步以下字符串代表DOS 命令

exec xp_cmdshell 'mkdir C:aaaaa'

create database S2218 --新建 数据库 名称

on primary --通向

(

name='S2218_data', --逻辑文件名

filename='c:aaaaaS2218_data.mdf', --物理文件名--挂盘符,有后缀

size=5mb, --初始大小

filegrowth=15% --文件增长量

)

log on

(

name='S2218_log',

filename='c:aaaaaS2218_log.ldf',

size=2mb,

filegrowth=1mb

)

go

create database S2220 --新建 数据库 名称

on primary --通向

--2.创建一个学生表Student***************************************************************    

Student(Sid,Sname,Sage,Sremark,Cid)

use S2218

--如何判定数据库中有没有某张表???sysobjects

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

--删除表

drop table Student

create table Student1 --创建表的基础语句‘Student’是表的名称

(

Sid int identity(1,1) primary key NOT NULL,

Sname nvarchar(32) not null,

Sage int NOT NULL,

Sremark nvarchar(255) not null default('无备注'),

Cid int not null,

address nvarchar(255) not null

)

--*********************************3.创建一个年级表 Grade(Cid,Cname)

-- 如果表存在,删除表

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

drop table grade

create table Grade

(

cid int identity(1,1) primary key not null,

cName nvarchar(20) not null

)

--******************************4.代码方式给Student表中添加条数据   

use S2218

insert into Grade(cName) values('精英班')

insert into Grade(cName) values('牛X班')

insert into Grade(cName) values('冲刺班')

select* from grade

insert into Student(Sname,Sage,Sremark,Cid,address) values('李小龙',1,'xxxxx',1,'xxxxxx')

--****************************************5.添加主键约束

给Student表添加主键约束,如果有,删除原约束再添加

给Grade表添加主键约束 ,如果有 ,删除再添加

select * from sysobjects

where type='k' and name='pk_sid'

--Grade表添加一个逐渐

alter table grade

add constraint PK_cid primary key(cid)

alter table Student

add constraint pk_Sid primary key(Sid)

alter table Student

drop constraint PK__Student__CA1E5D7808EA5793

--******************************** 6.添加外键约束

/*常见的约束:

          1:主键约束

          2:唯一约束

          3:检查约束

          4:外键约束

          5:默认约束

          6:非空约束

       */

   谁是主键表谁是外键表

alter table student --student代表从表

add constraint FK_foreign

foreign key(cid) references grade(cid)

--*******************7.添加唯一约束,保证学生姓名唯一

use S2218

alter table student

add constraint uq_sname unique (sname)

select * from Student1

insert into Student(Sname,Sage,Sremark,address,Cid)values('小明','20','北京啊','北京',1)

--********************8.添加默认约束(备注默认值为:无备注)

alter table Student

add constraint DF_Sremark default('无备注') for Sremark

insert into Student1(Sname,Sage,address,Cid)values('小明','20','北京',1)

--**********************9.添加检查约束学生年龄>=18岁

alter table Student

add constraint CK_Sage Check(Sage>=18)

update Student set Sage=18

where Sage=20

select * from Student

        10.删除数据库,删除表,删除约束

--回闪

--删除约束

alter table Student

drop constraint PK_SID

--删除表

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

drop table student1

--删除数据库

use master

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

drop database s2218ss

推荐阅读