Mysql(三)约束
一、视图
视图是虚拟的数据表,本身不存储数据,而是提供数据的逻辑 展示。
1、创建视图
create view stu_view as
select s1.id, s1.name, s2.room, s2.stay_time
from student s1, stay s2
where s1.id = s2.id;
创建视图后,就可以像查询数据表一样查询视图。 视图的字段就是我们从数据表中查询而来的字段。
select id from stu_view desc stu_view
2、修改视图
alter view stu_view as
select s1.id, s1.name, s2.room, s2.stay_time
from student s1 left outer join stay s2
on s1.id = s2.id;
select * from stu_view
3、删除视图
drop view stu_view;
4、 在修改视图时,我们可以不使用alter,而是or replace。 在视图不存在时,创建视图,当视图存在时,替换视图。
create or replace view stu_view as
select s1.id, s1.name, s2.room, s2.stay_time
from student s1 left outer join stay s2
on s1.id = s2.id;
5、视图只是一个预存储的查询语句。(as后面的查询语句) 当我们查询视图时,视图的查询语句就会展开。(就是从 视图存储的查询语句(结果集)中查询。
select * from stu_view
select * from (
select s1.id, s1.name, s2.room, s2.stay_time
from student s1 left outer join stay s2
on s1.id = s2.id) x;
视图的特征:
1 视图作为预存储的查询语句,不能提高性能。
2 视图可以简化我们的输入操作。
3 视图可以提供相同数据表的不同逻辑展示。
二、
视图默认情况下,是以我们查询的字段命名。
#当视图的查询字段出现同名时(命名冲突)时,我们可以:
#1 使用别名 当使用别名时,视图字段会以别名来命名。
#2 自定义视图字段的名称
1、别名
create or replace view stu_view as
select s1.id stu_id, s2.id stay_id, s1.name, s2.room, s2.stay_time
from student s1 left outer join stay s2
on s1.id = s2.id;
desc stu_view;
2、自定义视图字段的名称,指定的字段数量与查询的字段数量必须一致。
create or replace view stu_view(field1, field2) as
select s1.id, s2.stay_time
from student s1 left outer join stay s2
on s1.id = s2.id;
desc stu_view;
三、索引
索引是数据表中一个特殊的对象。
优点:索引可以加快记录的查询速度。
缺点:
#1 索引需要占用额外的硬盘空间。
#2 当数据表中记录发生变动时(增加,修改等),索引需要 进行重新变更(重新维护)。
索引使用的场合:
1 数据量很大(表中的记录多)
2 查询操作多
3 增加,删除等操作少。
1、创建索引
create index stu_index on student(id);
create index 索引名 on 表名(字段1,字段2, ……字段n); 可以指定一列或者多列
2、删除索引
drop index 索引名 on 表名
drop index stu_index on student
索引这就类似于图书的目录一样,如果我们想要查询某些内容, 我们可以从第一页开始,逐页进行查找,但这势必会耗费大 量的查找时间。但是,如果我们通过目录来查询,就可以快 速的定位到相关的页码上。
四、数据库约束
约束
约束,就是一种限制,其可以保证数据的正确性与完整性。
约束可以分为如下几类:
1唯一性约束
2非空约束
3主键约束
4外键约束
5检查约束
约束具有字段级语法与表级语法。
1、唯一性约束
唯一性约束保证约束列(一列或多列)的值不能出现重复。唯一性约束允许加入多个null值。因为MySQL中,null不等于任何值,包括其自身。因此,多个null值,彼此也是不等的。
字段级语法:
如果不存在创建表
create table if not exists t(
id int primary key,
age int unique
);
如果表存在,删除。
drop table if exists t;
insert into t(id, age) values (1, 1);
#错误,违法唯一性约束。
#insert into t(id, age) values (2, 1);
insert into t(id, age) values (3, null);
#允许插入多个null值。
insert into t(id, age) values (4, null);
select * from t;
表级语法:
唯一性约束也可以作用于多个字段。当作用于多个字段时, 只要多个字段的值不全相等,则认为是不重复的。
create table t(
id int primary key,
age int,
name varchar(20),
unique key (age, name)
);
与表级语法unique key (age, name)不同,对于表级语法, age,name只要有一个字段值不同即可,对于字段级语法, age与name两个字段值都不允许重复。
在建表之后增加唯一性约束:
对于建表之后加入的约束,一定要保证当前表中的数据 没有破坏该新增的约束,否则,约束就无法加入成功。
alter table t add unique key(age, name);
alter table t modify age int unique;
删除唯一性约束
alter table t drop index age;
2、非空约束
非空约束表示字段不允许为null值,该约束只有字段级语法,没有表级语法。
create table t(
id int primary key,
age int not null
);
在建表之后指定非空约束
create table t(
id int primary key,
age int
);
alter table t modify age int not null;
删除(取消)非空约束。
alter table t modify age int null;
3、主键约束
主键字段既不能为null,也不能重复。主键约束就是唯一性约束+非空约束。
字段级语法:
create table t (
id int primary key
);
表级语法:
create table t (
id int,
name varchar(10),
primary key(id, name)
#我们可以给主键命名,但仅仅是语法上支持,功能上不支持。
#不管我们如何命名,MySQL主键名都是primary。
#primary key pk(id, name)
);
当我们使用多个字段充当主键(联合主键),作为主键的多个字段只要不同时相同,就认为是不重复的,但是,每个字段都不允许为null。
建表之后增加主键:
create table t(
id int
);
alter table t add primary key(id);
alter table t modify id int primary key;
删除主键:
alter table t drop primary key;
4、外键约束:
如果B表中B1字段参照A表中的A1字段,则我们称B表为从表,A表为主表。B1字段的值或者为null,或者必须是A1字段中存在的值。A1字段必须是主键约束,或者是唯一性约束。
图书表
create table book(
id int primary key,
name varchar(30),
author varchar(30)
);
借书表
#字段级语法,MySQL仅支持语法,不支持功能。
create table borrow(
id int primary key,
book_id int references book(id),
borrow_person varchar(30)
);
#表级语法,MySQL支持
drop table if exists borrow;
create table borrow(
id int primary key,
book_id int,
borrow_person varchar(30),
foreign key(book_id) references book(id)
#也可以自定义外键的名字。
#constraint fk foreign key(book_id) references book(id)
);
insert into book(id, name, author) values(1, 'Java', 'abc');
insert into book(id, name, author) values(2, 'C++', 'def');
insert into book(id, name, author) values(3, 'C#', '张三');
insert into book(id, name, author) values(4, 'Hadoop', '小李');
select * from book;
insert into borrow(id, book_id, borrow_person)
values (1001, 3, '学生A');
#错误,违反外键约束。
#insert into borrow(id, book_id, borrow_person)
#values (1002, 5, '学生B');
insert into borrow(id, book_id, borrow_person)
values (1002, null, '学生B'); update book set id=10 where id=3
delete from book where id=3;
在建表之后增加外键:
create table borrow(
id int primary key,
book_id int,
borrow_person varchar(30)
);
alter table borrow add foreign key(book_id) references book(id);
指定外键名
alter table borrow add constraint fk foreign key(book_id) references book(id);
删除外键
alter table borrow drop foreign key fk;
当主表的某条记录被从表所参照时,当主表记录修改或删除时,
从表的表现方式(行为):
1restrict 当主表记录修改或删除时,拒绝执行。
2cascade 当主表记录修改或删除时,从表随之也修改或删除。
3set null 当主表记录修改或删除时,从表记录设置为null值。
4no action 等价于restrict
默认的行为为:restrict
5、检查约束(MySQL仅支持语法,不支持功能)
create table t(
id int primary key,
age int,
check (age > 0)
);
insert into t (id, age) values (1, -2);
select * from t;
约束的表级语法与字段级语法。
1 相对于表级语法,字段级语法更简单些。
2 字段级语法只能作用于单个字段,而表级语法可以作用于多个 字段。例如:联合主键。
3 字段级语法不能为约束命名,而表级语法可以为约束命名。
五、union
union 用于合并多个结果集。
要求多个结果集的字段类型与字段数量一致。
union distinct 会去掉结果集中的重复记录。
union all 不会去掉结果集中的重复记录。
默认为union distinct
优先考虑使用union all(性能会好一些)。
select * from student
select * from stay;
select id from student union select id from stay;
select id from student union all select id from stay;
六、子查询
子查询即查询中还有查询(嵌套查询)
根据子查询出现的位置,可以将子查询分为两类:
1 出现在from之后,作为临时的数据表。
2 出现在where(having)之后,作为过滤条件。
子查询需要使用()括起。
子查询根据查询结果记录条数,可以将子查询分为:
1 单行子查询 返回一条记录
2 多行子查询 返回多条(一条)记录。
select id from (select id, name from student) x;
#查询与张三年龄相等的学生
#select id, name from student where age = 张三的年龄
#张三(id为1)的年龄?
#select age from student where id = 1;
#改进:
#select id, name from student where age =
#(select age from student where id = 1)
select * from student
select age from student where id = 2;
select id, name from student where age = 20;
select id, name from student where age =
(select age from student where id = 2);
#=, >, <, >=, <=, <>(!=),要求后面的子查询是单行子查询
#即最多只能返回一条记录。
#错误
#select id, name from student where age =
#(select age from student )
#any 表示任意一个(随便一个)
select id from student where id >
any (select id from stay)
#大于任意一个,相当于大于最小的。
select id from student where id >
(select min(id) from stay);
#all表示所有的
select id from student where id >
all (select id from stay);
#大于所有的,相当于大于最大的。
select id from student where id >
(select max(id) from stay);
#some 任意一个,等价于any
#in 在集合中(与集合中任意一个值相等)
select id from stay
select id from student where id in (1, 2, 3);
select id from student where id in
(select id from stay);
#exists 返回true或false,子查询有记录返回true,没有记录返回false。
select id from student s where exists
(select id from stay where id = s.id)
Mysql(三)约束的更多相关文章
- MySQL(三) —— 约束以及修改数据表
约束: 1. 约束保证数据的完整性和一致性: 2. 约束分为表级约束和列级约束: 3. 约束类型包括:NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREI ...
- MySQL(三)
MYSQL(三) 上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: MYSQL入门全套(第一部) MYSQL入门全套(第二部) 索引 ...
- MYSQL数据库约束类型
07.14自我总结 MYSQL数据库约束类型 一.主键约束(primary key) 主键约束要求主键列的数据唯一,并且不能为空.主键分为两种类型:单字段主键和多字段联合主键. 1.单字段主键 写法 ...
- MYSQL中约束及修改数据表
MYSQL中约束及修改数据表 28:约束约束保证数据的完整性和一致性约束分为表级约束和列级约束约束类型包括: NOT NULL(非空约束) PRIMARY KEY(主键约束) UNI ...
- mysql(三) 数据表的基本操作操作
mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...
- 【MySQL】MySQL的约束
在开始之前,笔者介绍一下笔者使用的数据库版本为5.7.所有的关系型数据库都支持对数据表使用约束,通过约束可以更好的保证数据表里数据的完整性.约束是在表上强制执行的数据校验,约束主要用于保证数据库里数据 ...
- mysql的约束
SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). (1)NOT NULL约 ...
- MySQL三种存储引擎总结
MySQL三种存储引擎 MyISAM.InnoDB.MEMORY 1.MyISAM MyISAM,3.23.34a前的默认存储引擎. 优缺点 优点 在于占用空间小,处理速度快. 缺点 不支持事务的完整 ...
- [转]mysql的约束
转自:http://blog.csdn.net/kqygww/article/details/8882990 MySQL中约束保存在information_schema数据库的table_constr ...
随机推荐
- Android 下拉刷新上拉加载PullToRefresh
https://github.com/823546371/PullToRefresh http://www.jianshu.com/p/0f5d0991efdc
- 20155206 Exp2 后门原理与实践
20155206 Exp2 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP 然后使用ncat.exe程序,ncat. ...
- 全虚拟化和半虚拟化的区别 cpu的ring0 ring1又是什么概念? - 转
http://www.cnblogs.com/xusongwei/archive/2012/07/30/2615592.html ring0是指CPU的运行级别,ring0是最高级别,ring1次之, ...
- Kafka查看topic、consumer group状态命令
最近工作中遇到需要使用kafka的场景,测试消费程序启动后,要莫名的过几十秒乃至几分钟才能成功获取到到topic的partition和offset,而后开始消费数据,于是学习了一下查看kafka br ...
- JSON传输数组的基本操作
目标JSON结果如下: 生成JSON的的过程如下: Document document; Document::AllocatorType& allocator = document.GetAl ...
- BERT总结:最先进的NLP预训练技术
BERT(Bidirectional Encoder Representations from Transformers)是谷歌AI研究人员最近发表的一篇论文:BERT: Pre-training o ...
- libgdx学习记录9——FreeType,ttf中文显示
前面讲到使用Hireo创建的BitmapFont以显示中文字体.这种方式效率很高,当所要显示的字的总数较少,并且大小比较固定时,可以采用这种方式. 但是这种也有弊端: (1)字体大小不能随意设置,当放 ...
- 行级安全(Row-Level Security)
通过授予和拒绝(Grant/Deny)命令控制用户的权限,只能控制用户对数据库对象的访问权限,这意味着,用户访问的粒度是对象整体,可以是一个数据表,或视图等,用户要么能够访问数据库对象,要么没有权限访 ...
- 贪心Crossing river
英文题目: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...
- Assetbundle管理与加载
最近在做项目优化的时候发现公司的项目用的还是老式的WWW去加载assetbundle资源的形式,而且是通过在两个Update里面分开加载AB和Asset的,这样虽然避免了协程的的使用,但是把一件事分开 ...