关系型数据库学习(三):约束、数据库的备份与还原

本文主要内容:约束、数据库的备份与还原

约束

* 概念: 对表中的数据进行限定,保证数据的正确性、有效性和完整性
* 分类:
	1. 主键约束: primary key
	2. 非空约束: not null
	3. 唯一约束: unique
	4. 外键约束: foreign key
* 非空约束: not null, 值不能为空
	1. 创建表时添加约束:
		create table student(id int, name varchar(5) not null);
	2. 修改表的属性为非空:
		alter table student modify name varchar(5) not null;
	3. 删除表的非空约束:
		alter table student modify name varchar(5);

* 唯一约束: unique, 值不能重复
	1. 创建表时添加约束:
		create table student(id int, phone varchar(11) unique);
		注意:MySQL中, NULL值不受unique限定
	2. 修改表的属性为唯一约束:
		alter table student modify phone varchar(11) unique;
	3. 删除表的唯一约束:
		alter table student drop index phone;

* 主键约束: primary key
	1. 注意:
		1. 含义: 非空且唯一
		2. 一张表只能有一个字段为主键
		3. 主键就是表中记录的唯一标识
	2. 创建表时,添加主键约束:
		create table student(id int primary key, name varchar(20));
	3. 修改表的属性为主键约束:
		alter table student modify id int primary key;
	4. 删除表的主键约束:
		alter table student drop primary key;
	5. 自动增长:
		1. 概念:如果某一列是数值类型,使用auto_increment来完成自动增长
		2. 在创建表的时候,创建主键为自动增长:
			create table student(id int primary key auto_increment, name varchar(5));
		3. 删除自动增长:
			alter table student modify id int;
		4. 添加自动增长:
			alter table student modify id int auto_increment;

* 外键约束: foreign key, 让表于表产生关系, 保证数据正确性
	1. 在创建表时,添加外键:
		* 语法:
			create table 表名(
					...
					外键列, 
					foreign key (外键列名) references 主表名称(主表列名)
				);
	2. 删除外键:
		alter table 表名 drop foreign key 外键名;
	3. 添加外键:
		alter table 表名 add  foreign key (外键列名) references 主表名称(主表列名);
	4. 级联操作:
		1. 添加级联操作
			语法: alter table 表名 add foreign key (外键列名) references 主表名称(主表列名) on update cascde on delete cascde;
		2. 分类:
			1. 级联更新: on update cascde
			2. 级联删除: on delete cascde    //注意使用

数据库的备份与还原

1. 命令行:
	* 备份: mysqldump -u用户名 -p密码 数据库名 > 保存的路径
	* 还原: 
		1. 登陆数据库
		2. 创建数据库
		3. 使用数据库
		4. 执行文件: source 文件路径;
2. 图像化工具
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值