数据库的实现

2.1 使用SQL语句管理数据库

一,创建数据库

  create database 数据库名称

  [ON[primary]]

  [

    <数据文件参数>[,….n]

    [,<文件组参数>[,….n]

  ]

  [log on{<日志文件参数>[,….n]}]

  ]

carate database t123
on
(
name=t123_mdf,
filename='E:\t123_mdf.mdf',
slze=3MB,
maxsize=100MB,
filgrowth=1MB
)
log on
(
name=t123_ldf,
filename='E:\t123_ldf.ldf',
slze=3MB,
maxsize=100MB,
filgrowth=1MB
)
go

二,删除数据库

if DB_ID('t123') is not null

  drop database t123

go

三,数据库的类型

1.字符型数据:char(size),varchar(size)

2,数字型数据:bit,int,float,numeric(m,n)

3,日期时间类型:dateTime

4,二进制数据类型:text,image

5,货币数据类型:money,smallmoney

6,Unicode:nchar(size),nvarchar(size),ntext(size)

四,创建表

ccreate table 表名

(  

  字段1 数据类型 字段特征, 

)

create table t_info
(
  t_id int not null identity(1,1),--会员编号
 t_number nvarchar(50) not null,  --会员号
 ..........       
)

1.添加列

alter table t_info

  add t_age varchar(50) null

go

alter table t_info

  add t_age varchar(50) null,
            t_sex int null

go

2,删除列

alter table 表名

  drop column {字段}[…..n]

alter table t_info
    drop column t_age
go

3,修改列

alter table 表名

  alter column 字段 数据类型[null | not null]

  

alter table t_info

  alter column t_age declmal(..n)
go

五,删除表

if exists(select*from sys.sysobjects where name='t_info')
    drop table t_info
go

六,数据完整性的分类

1,域完整性

域即是列(字段).

2,实体完整性

行完整性

3,参照完整性

引用完整性(表与表之间的引用)

4,用户定义完整性

2和3是关系模型最基本的要求,用户可以定义一些特殊的约束条件.

七,约束

1,主键约束(primary key)

alter tabke 表名

  add constralnt 约束名称 primary key(字段[…n])

2,唯一约束(unlpue)

alter tabke 表名

  add constralnt 约束名称 unlpue(字段[…n])

3,默认约束(default)

alter tabke 表名

  add constralnt 约束名称 default 默认值 for 字段

4,检查约束(check)

alter tabke 表名

  add constralnt 约束名称 check(逻辑表达式)

5,外键约束()

alter tabke 表名

  add constralnt 约束名称 primary key(从表字段)pefferences主表表名(主表字段)

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注