数据库的创建

来源:互联网 发布:adobe xd mac 破解版 编辑:程序博客网 时间:2024/06/07 06:43

创建数据库
-- 指定数据库名称
-- (注:如果数据库名中包含空格可以使用[]将其标示)
create database [Super WC]
-- 关于数据文件的定义
on
(
name = Super_WC_Data,                -- 逻辑名
filename = 'C:/Super_WC_Data.MDF',        -- 物理路径以及物理名
size = 2MB,                    -- 初始大小
maxsize = 4MB,                    -- 最大限制
filegrowth = 1MB                                            -- 增长大小
)
-- 关于日志文件的定义
log on
(
name = Super_WC_Log,
filename = 'C:/Super_WC_Log.LDF',
size = 3MB,
maxsize = 7MB,
filegrowth = 20%                                            -- 增长比例
)

-- 附加数据库
execute sp_attach_db '[Super WC]', 'C:/Super_WC_Data.MDF','C:/Super_WC_Log.LDF'
-- 分离数据库
execute sp_detach_db '[Super WC]'
-- 复制数据库
execute master.dbo.xp_cmdshell 'copy C:/Super_WC_Data.MDF D:/Super_WC_Data.MDF'
execute master.dbo.xp_cmdshell 'copy C:/Super_WC_Log.LDF D:/Super_WC_Log.LDF'


(1)创建数据表

创建一个数据表:学生(students)
结构如下:
字段       类型        是否允许为空     约束           备注
no         char(4)     No               主键           学号

name       nvarchar(8) No               唯一           姓名

birthday   datetime    No               检查(至少18年前) 生日

age        tinyint     No               缺省(默认等于当前时间减去生日) 年龄

sex        nchar(1)    No               缺省(默认'女')                                 性别

phone      char(11)    Yes              检查(要么没有,要么长度等于11) 电话

address    nvarchar(24)No                                地址

没有特别约束的情况:
create table student
(
no        char(4)        not null,
name      nvarchar(8)        not null,
birthday  datetime        not null,
phone     char(11)        null,
address   nvarchar(24)        null
)

注意:没有特别约束的情况下,创建数据表可以参考“企业管理器”中“设计表”的操作格式!

包含约束的情况:
create table students
(
no        char(4)         primary key,
name      nvarchar(8)         unique,
birthday  datetime         check(datediff(year, birthday, getdate()) >= 18),
age       as datediff(year, birthday, getdate()),
sex      nchar(1)        default('女') check(sex = '女' or sex = '男')
phone     char(11)         check((phone is null) or (len(phone) = 11)),
address   nvarchar(24)
)


create table scores
(
no                char(4)                foreign key references students(no),
chinese        numeric(4,1)    check(chinese >= 0 and chinese <= 100),
english        numeric(4,1)    check(english >= 0 and english <= 100)    
)

以上答案只是最简单的描述形式!

比较规范的写法是
先用create table声明数据表的结构,

CREATE TABLE students
(
no       char(4),
name     nvarchar(8),
birthday datetime,
age      as DATEDIFF(year, birthday, getdate()),
sex             nchar(1),
phone    char(11),
address  nvarchar(24)
)

然后再ALTER TABLE  ADD CONSTRAINT 分别指定每个字段的约束:
每个约束都有一个独特的名称,其中,命名规范采用以下格式:
约束类型的简写_表名_字段名
pk_students_no

ALTER TABLE students
ADD CONSTRAINT pk_students_no PRIMARY KEY (no)

ALTER TABLE students
ADD CONSTRAINT uq_students_name UNIQUE (name)

ALTER TABLE students
ADD CONSTRAINT ck_students_birthday CHECK (datediff(year,[birthday],getdate()) >= 18)

ALTER TABLE students
ADD CONSTRAINT df_students_sex default ('女') for sex

ALTER TABLE students
ADD CONSTRAINT ck_students_sex CHECK ([sex] = '男' or [sex] = '女')

ALTER TABLE students
ADD CONSTRAINT ck_students_phone CHECK ([phone] is null or len([phone]) = 11)

相对应的对约束进行删除,则是通过DROP CONSTRAINT子句来完成:
ALTER TABLE students
DROP CONSTRAINT pk_students_no

ALTER TABLE students
DROP CONSTRAINT uq_students_name

注意:
约束只有添加与删除操作,没有修改操作!


注意:
其中,age(年龄)采用了“计算列”的表示方法!
“计算列”的定义:
在表中某个字段的值源于某一表达式的值(某一函数的运算结果或是其他字段的运算结果)!
比如:
某学生的成绩表结构如下:
数学
语文
体育
总分

创建命令如下:
create table scores
(
math      numeric(3, 1),
chinese  numeric(3, 1),
sport     numeric(3, 1),
total      as math + Chinese + sport
)

insert into scores values (80, 69, 95)

total 部分的值会自动计算,为 244

-- 创建临时表
-- 临时表将会存储在TempDB的临时数据库中
-- 当用户断开连接后,就会自动删除
-- 语法:在表名前加上#
create table #tt
(
a int,
b int
)

insert into #tt values (1,1)
insert into #tt values (2,2)

select * from #tt

 

(2)数据操作(添加/删除/修改)

添加操作(insert)的语法格式:
Insert [into] 数据表 (字段) values (数据)

-- 添加记录(一般情况)
insert into students
(no,name,birthday,sex,phone,address)
values
('0001', 'AHuang', '2000-01-01', '男', '13307331100', '株洲')

(注意: into 可以省略 )


-- 添加记录(如果是给所有字段添加数据,可以省略字段标示)
insert into students
values
('0002', 'ANing', '2008-08-08', '女', '13307330011', '北京')


-- 添加记录(如果是给具有默认约束的字段添加数据,想利用默认约束,可以利用default)
insert into students
values
('0002', 'ANing', '2008-08-08', default, '13307330011', '北京')


删除操作(delete)的语法格式:
Delete [from] 数据表 where 条件

-- 删除记录(删除所有)
delete from students

(注意: from 可以省略,即可以: delete students )

-- 删除记录(删除特定记录,可以通过 where 条件来过滤,比如:学号'0001'的学生记录)
delete from students where no = '0001'


修改操作(update)的语法格式:
update 数据表 set 字段 = 新值 where 条件

-- 修改记录(修改所有)
update students set 性别 = '女'

-- 修改记录(修改特定记录,可以通过 where 条件来过滤,比如:学号'0001'的学生记录)
update students set 性别 = '男' where no = '0001'

原创粉丝点击