一、非空约束 not null
非空约束可以加在诸如姓名等列上
>CREATE TABLE student(
student_id INT,
student_name VARCHAR(10) NOT NULL,
student_birthday DATETIME,
student_phone INT,
student_score FLOAT);
>desc student
删除非空约束
alter table student modify student_name varchar(10) null;
二、域完整性-默认约束 default
现给学生表加入性别列,默认值设为“男”,这样添加新的学生信息时如果没有填写具体的性别均会默认为男性:
>alter table student add student_sex varchar(5) default ‘男’;
>CREATE TABLE student(
student_id INT,
student_name VARCHAR(10) NOT NULL,
student_sex VARCHAR(5) DEFAULT '男',
student_birthday DATETIME,
student_phone INT,
student_score FLOAT);
修改字段默认值
alter table 表名 alter column 字段名 drop default; --(若本身存在默认值,则先删除)
alter table 表名 alter column 字段名 set default 默认值; --(若本身不存在则可以直接设定)
三、实体完整性-唯一约束 unique key
唯一约束是指给定列的值必须唯一,与主键约束不同的是它可以为空。通常加在表中不能重复的信息中,如电话号码
>alter table student add unique key(student_phone)
>CREATE TABLE student(
student_id INT,
student_name VARCHAR(10) NOT NULL,
student_sex VARCHAR(5) DEFAULT '男',
student_birthday DATETIME,
student_phone INT unique,
student_score FLOAT);
四、实体完整性--主键约束 primary key
主键列不能为空也不能重复,通常加在表的id列中
>alter table student add primary key(student_id)
>CREATE TABLE student(
student_id INT primary key,
student_name VARCHAR(10) NOT NULL,
student_sex VARCHAR(5) DEFAULT '男',
student_birthday DATETIME,
student_phone INT unique,
student_score FLOAT);
删除主键约束
alter table student drop primary key;
五、引用完整性--外键约束
外键约束是指在外键关联主键上强制加上一个约束,如果违反该约束,则不允许该条数据的修改。
创建主表--班级表
CREATE TABLE class(
class_id INT PRIMARY KEY,
class_name VARCHAR(20) UNIQUE NOT NULL);
)
创建从表--学生表,并设置外键约束
CREATE TABLE student(
student_id INT PRIMARY KEY,
Class_id int,
student_name VARCHAR(10) NOT NULL,
student_sex VARCHAR(5) DEFAULT '男',
student_birthday DATETIME,
student_phone INT UNIQUE,
student_score FLOAT
)
添加外键
alter table student add constraint FK_ID foreign key(class_id)
References class(class_id)
查看外键
>show create table student;
删除外键
ALTER TABLE student DROP FOREIGN KEY `FK_ID`
为已经添加好的数据表添加外键
语法:alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字段名);
例:
alter table tb_active add constraint FK_ID foreign key(user_id) REFERENCES tb_user(id)
//FK_ID是外键的名称
/*
CREATE TABLE `tb_active` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `user_id_2` (`user_id`),
CONSTRAINT `FK_ID` FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
*/
删除外键
语法: ALTER TABLE table-name DROP FOREIGN KEY key-id;
例: ALTER TABLE `tb_active` DROP FOREIGN KEY `FK_ID`
ALTER TABLE:添加,修改,删除表的列,约束等表的定义
• 查看列:desc 表名;
• 修改表名:alter table t_book rename to bbb;
• 添加列:alter table 表名 add column 列名 varchar(30);
• 删除列:alter table 表名 drop column 列名;
• 修改列名MySQL: alter table bbb change nnnnn hh int;
• 修改列属性:alter table t_book modify name varchar(22);
sp_rename:SQLServer 内置的存储过程,用与修改表的定义。
MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列
• 查看表的字段信息:desc 表名;
• 查看表的所有信息:show create table 表名;
• 添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
• 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
• 删除主键约束:alter table 表名 drop primary key;
• 删除外键约束:alter table 表名 drop foreign key 外键(区分大小写);
• 修改表名:alter table t_book rename to bbb;
• 添加列:alter table 表名 add column 列名 varchar(30);
• 删除列:alter table 表名 drop column 列名;
• 修改列名MySQL: alter table bbb change nnnnn hh int;
• 修改列名SQLServer:exec sp_rename't_student.name','nn','column';
• 修改列名Oracle:alter table bbb rename column nnnnn to hh int;
• 修改列属性:alter table t_book modify name varchar(22);
Comments