国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

面試準備之SQL 2——數據庫的實現(T-SQL建庫建表)

2019-11-03 08:34:25
字體:
來源:轉載
供稿:網友

1. 數據庫文件包括:

主數據文件:*.mdf

次要數據文件:*.ndf

日志文件:*.ldf ( l 是 L 的小寫)

 

2.使用T-SQL創建數據庫

 

代碼
use master
go
-----------創建數據庫------------

if exists (select * from sysdatabases where name='stuDB')
drop database stuDB
create database stuDB
on size=3mb,
maxsize=10mb,
filegrowth=1mb
)
log on
(
name='stuDB_log',
filename='D:/stuDB_data.ldf',
size=1mb,
filegrowth=1mb
)

 

 

3.使用T-SQL 創建數據庫表

 

代碼
-----------創建數據庫表------------
use stuDB
go
if exists (select * from sysobjects where name='stuInfo')
drop table stuInfo
create table stuInfo
(
    stuName varchar(20) not null,
    stuNo char(6) not null,
    stuAge int not null,
    stuID numeric(18,0),--身份證
    stuSeat smallint identity(1,1),
    stuAddress text
)
go

if exists (select * from sysobjects where name='stuMarks')
drop table stuMarks
create table stuMarks
(
    ExmaNo char(7) not null, --考號
    stuNo char(6) not null,--學號
    writtenExam int  not null,--筆試成績
    LabExam int not null--機試成績
)
go
 

 

4. 添加約束

 

代碼
--------------添加約束-----------------

alter table stuinfo --修改stuinfo表
add constraint PK_stuNo primary key (stuNo)--添加主鍵 PK_stuNo是自定義的主鍵名 也可以省略

alter table stuinfo
add constraint UQ_stuID unique (stuID) --添加唯一約束

alter table stuinfo
add constraint DF_stuAddress default ('地址不詳') for stuAddress--添加默認  不填默認’地址不詳‘

alter table stuinfo
add constraint CK_stuAge check(stuAge between 18 and 60) --添加檢查約束 18-60歲

alter table stuMarks
add constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)
go
 

 

5.刪除約束

 

-------------刪除約束--------------
alter table stuinfo
drop constraint 約束名 --如:FK_stuNo  CK_stuAge DF_stuAddress UQ_stuID PK_stuNo

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庐江县| 大埔县| 额尔古纳市| 海宁市| 利津县| 潼南县| 光泽县| 鹤岗市| 扶余县| 涟源市| 阜新| 海安县| 古田县| 阳曲县| 内丘县| 嘉义县| 永寿县| 高安市| 延吉市| 深州市| 建瓯市| 南开区| 华宁县| 东乌珠穆沁旗| 寻乌县| 揭阳市| 斗六市| 广丰县| 湘潭市| 镇雄县| 咸丰县| 江北区| 咸宁市| 紫金县| 梁河县| 达拉特旗| 澄江县| 汉沽区| 图木舒克市| 淳化县| 民乐县|