跟随大学课程从0开始_小白_初学数据库_MySQL(2)_创建并管理表

在企业管理器中创建表

在企业管理器中创建:展开数据库,右键点击表-〉新建表,弹出对话框中输入表属性列,完成后关闭对话框同时对表起名。之后在窗口中右键选择新建的表-〉打开表,输入表中的数据。

一些特殊字段说明

Image:字段需要存放超过8kb且为可变长度的二进制数据。包括:word文件,excel表格,位图,gif、jpeg、tiff等图片,声音,或ole对象等。Sqlserver对所存储数据不作解释,若想正确解析数据,需要通过各种客户端应用程序。
Unicode数据类型(nchar、varnchar、ntext):采用unicode编码存储字符,每个字符占2个字节。

整型数据:bigint(8字节,范围-2^63-1 ~ 263-1);int(4字节,范围:-231-1 ~ 231-1);smallint(2字节,范围:-215-1 ~ 2^15-1);tinyint(1字节,范围0-255)

精确数字:numeric\decimal,可以指定精度,和小数位数。
Decimal(精度[,小数位数]) 小数位数可以省略

timestamp:时间戳,该类型数据为一组二进制数据,表示数据库发生修改的先后顺序与次数,与日期类型date毫无关连。每个表只能有一个该类型字段,并且该字段不能定义为主键(PrimaryKey)。

Table:该类型不能在表的字段中定义,只能用户定义变量或函数时使用该类型,用来返回一个临时结果集。
自动增长(自动编号)字段:为表的每一条记录进行流水编号,并随记录的递增而自动增长。Sqlserver没有额外提供自动增长字段,需要自行定义。能够成为自动增长字段的数据类型必须是整型数据,并指定其基本值与递增量。在企业管理器中,对欲将其设为自动增长字段的属性卡中-〉标识-〉是,然后指定标识种子(基本值)与标识递增量。

NULL值:不代表0值,空字符串或空白,而表示值不存在。字段允许null,表示该字段未知或尚未定义,而且该字段不拥有默认值。某些字段不允许null,比如自动编号字段或主键约束字段等。不允许null值,可以使用户务必在字段中输入数据,保证字段非空,从而使系统数据完整性更容易维护。

查询分析器中创建新表

use stu                  --使用当前新建数据库
go
create table student(        								--创建student表
student_id char(8) not null ,
student_name char(8) not null ,
sex char(2) not null,
birth smalldatetime not null,
class_id char(6) not null,
entrance_date smalldatetime not null)

指定自动增长字段

create table student(        
student_id int identity(1,1) not null ,  --indentity(初始值,步长)
student_name char(8) not null ,
sex bit not null,
birth smalldatetime not null)

创建计算字段

USE student
GO
create table grade
(
classname varchar(10) not null,
math numeric(3,1),
english numeric (3,1),
cprograme numeric(3,1),
total as math+english+cprograme,
average as (math+english+cprograme)--不可用 total/3 因为total已经调用了其他变量
)

显示及修改表的结构

--sp_help [表名] 显示表的结构

--exec sp_help stu
--alter table tabname add column col type 添加属性列
--alter table [表名] add 列名1 属性,列名2 属性
alter table stu add lover char(8) null
--delete [表名]清空表
--drop [表名] 删掉表 
--drop column [列名1,列名2...]删除列
alter table stu
drop column lover

为数据表指定主外键约束

use stu                  使用当前新建数据库
go
create table student(        
student_id char(8) not null primary key,   primary key:建表的同时指定表的主键
student_name char(8) not null ,
sex bit not null,
birth smalldatetime not null,
class_id char(6) not null,
ntrance_date smalldatetime not null)

create table class
(class_id char(6)not null primary key,
monitor nvarchar(8) null,
classroom varchar(13)null,
student_num smallint null default 0 )   default:为列添加默认值约束

create table student2
( student_id char(8) not null primary key,
student_name char(8) not null,
sex bit not null,
birth smalldatetime not null,
class_id char (6) not null references class(class_id),
class_idd char (6) not null constraint fkClassid foreign key references class(class_id),
ntrance_date smalldatetime not null)    

为已存在的表创建主外键约束

为已经建立的表添加主键约束:
添加主键: Alter table tabname add primary key(col) 
删除主键: Alter table tabname drop constraint(主键约束名)

constraint 约束名(fkclassid) foreign key references  表名class 列名 :以class_id列作为本表的外键,其引用值从表class中的class_id列获取。
alter table student
add constraint fkClassid foreign key (class_id) references class(class_id)

为数据表创建约束完整性条件

默认值约束 default:

如果字段拥有默认值,在添加数据时如未指定具体值,系统会将定义好的默认值填充。
Default定义:在创建表字段的同时,在其属性标签中指定默认值,仅在该字段起作用。
Default对象:右键单击“默认”图标,创建默认值对象,可以绑定多个属性列起作用。
基本语句:add constraint 约束名 default ‘常量表达式’ for 字段名
例:

alter table student       
add constraint defSex default 1 for sex 
为表 student 的sex字段添加默认值约束

创建表的同时规定默认值约束:
Create 表名 (列名 数据类型 CONSTRAINT 约束名 default 常数/表达式)
例:create table student  (sex bit constraint defSex default 0)

检查约束check

下面展示一些 内联代码片

alter table student_course
add constraint chkGrade check (grade >0 and grade<100)

alter table student
add constraint chkEntr check(entrance_date<getdate())
alter table student
add constraint chkBirth check(birth<getdate())
add constraint 约束名 check(具体规则):为已存在的表列添加检查约束
getdate():获取当前系统时间
alter table student
add constraint chkSex check(sex in ('m','f'))

alter table employee
add constraint chkPho check(phone like’([0-9] [0-9]) [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]	[0-9] [0-9]) ’ 
	or phone like’([0-9] [0-9]) [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]	[0-9] ) ’)

alter table employee
add constraint chkBlo check(bloodType in(’a’,’b’,’ab’,’o’,’rh+,’rh-) 

删除check约束

alter table 表名 drop constraint 约束名1,约束名2,……
alter table student drop constraint chkEntr,chkBirth,chkSex

主键约束

alter table student
add constraint  pkStudent_id primary key (student_id)
alter table class
add constraint  pkClass_id primary key (class_id)

add constraint  约束名 primary key (列名):在指定表的选定字段上设置主键约束

alter table student_course 
add constraint pkStu_Cou primary key (student_id,course_id)

注意事项

使用ALTER TABLE 语句的注意问题:
1、 如果需要同时修改多个字段特性,需要使用多条alter table语句;
2、 该语句不能更改或只能部分更改已经存在于索引中的字段、用来产生统计信息的字段、已经存在约束条件、默认值的字段等。
3、不能随意修改自动编号字段,也不能使用此语句将非自动编号字段转换为自动编号字段,如果必须完成此操作,需要到企业管理器中使用表设计器完成。或在查询分析器中使用删除字段drop子句及增加字段子句add分两步完成,前提示表中尚未存在数据。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值