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

首頁 > 開發(fā) > 綜合 > 正文

面試準(zhǔn)備之SQL 2——數(shù)據(jù)庫的實現(xiàn)(T-SQL建庫建表)

2024-07-21 02:45:14
字體:
供稿:網(wǎng)友

1. 數(shù)據(jù)庫文件包括:

主數(shù)據(jù)文件:*.mdf

次要數(shù)據(jù)文件:*.ndf

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

 

2.使用T-SQL創(chuàng)建數(shù)據(jù)庫

 

代碼
use master
go
-----------創(chuàng)建數(shù)據(jù)庫------------

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

 

 

3.使用T-SQL 創(chuàng)建數(shù)據(jù)庫表

 

代碼
-----------創(chuàng)建數(shù)據(jù)庫表------------
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,--學(xué)號
    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

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 肥西县| 平泉县| 兴安盟| 县级市| 全州县| 瓮安县| 子长县| 桦南县| 区。| 当涂县| 成都市| 藁城市| 屏南县| 永嘉县| 武隆县| 拜城县| 比如县| 西盟| 临泽县| 宜君县| 揭西县| 仁怀市| 巩义市| 神池县| 五华县| 庄河市| 游戏| 信阳市| 崇文区| 家居| 罗源县| 达州市| 西畴县| 乐都县| 苗栗县| 洛南县| 商丘市| 西华县| 株洲县| 六枝特区| 张北县|