一、视图

视图是虚拟的数据表,本身不存储数据,而是提供数据的逻辑 展示。

      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(三)约束的更多相关文章

  1. MySQL(三) —— 约束以及修改数据表

    约束: 1. 约束保证数据的完整性和一致性: 2. 约束分为表级约束和列级约束: 3. 约束类型包括:NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREI ...

  2. MySQL(三)

    MYSQL(三) 上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: MYSQL入门全套(第一部) MYSQL入门全套(第二部) 索引 ...

  3. MYSQL数据库约束类型

    07.14自我总结 MYSQL数据库约束类型 一.主键约束(primary key) 主键约束要求主键列的数据唯一,并且不能为空.主键分为两种类型:单字段主键和多字段联合主键. 1.单字段主键 写法 ...

  4. MYSQL中约束及修改数据表

    MYSQL中约束及修改数据表 28:约束约束保证数据的完整性和一致性约束分为表级约束和列级约束约束类型包括:    NOT NULL(非空约束)    PRIMARY KEY(主键约束)    UNI ...

  5. mysql(三) 数据表的基本操作操作

    mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...

  6. 【MySQL】MySQL的约束

    在开始之前,笔者介绍一下笔者使用的数据库版本为5.7.所有的关系型数据库都支持对数据表使用约束,通过约束可以更好的保证数据表里数据的完整性.约束是在表上强制执行的数据校验,约束主要用于保证数据库里数据 ...

  7. mysql的约束

    SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). (1)NOT NULL约 ...

  8. MySQL三种存储引擎总结

    MySQL三种存储引擎 MyISAM.InnoDB.MEMORY 1.MyISAM MyISAM,3.23.34a前的默认存储引擎. 优缺点 优点 在于占用空间小,处理速度快. 缺点 不支持事务的完整 ...

  9. [转]mysql的约束

    转自:http://blog.csdn.net/kqygww/article/details/8882990 MySQL中约束保存在information_schema数据库的table_constr ...

随机推荐

  1. python 带参数的多重继承

    1. 不带参数的多重继承 # 作者:hhh5460 # 时间:2017.07.18 class A(object): def show_x(self): print('A') class B(obje ...

  2. Zabbix使用总结

    1. CentOS 7上启动zabbix-server失败,/var/log/messages中的报错信息如下: Feb :: mysql-server1 systemd: Starting Zabb ...

  3. Spring+SpringMVC+MyBatis整合基础篇

    基础篇 Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简介 Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试 Spring+S ...

  4. 阿里云配置ssl证书

    一.申请证书和下载证书(阿里云申请) 二.在nginx服务器上配置ssl证书 1.检查服务器是否安装openssl 2.在nginx conf 文件夹创建 cret 文件,放置证书 [root@web ...

  5. 使用unity3d和tensorflow实现基于姿态估计的体感游戏

    使用unity3d和tensorflow实现基于姿态估计的体感游戏 前言 之前做姿态识别,梦想着以后可以自己做出一款体感游戏,然而后来才发现too young.但是梦想还是要有的,万一实现了呢.趁着p ...

  6. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

  7. Scrum立会报告+燃尽图(Final阶段第七次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2486 项目地址:https://coding.net/u/wuyy694 ...

  8. 词频统计 SPEC 20160911

    本文档随时可能修改,并且没有另行通知. 请确保每一次在开始修改你的代码前,读标题中的日期,如果晚于你上次阅读, 请重读一次. 老五在寝室吹牛他熟读过<鲁滨逊漂流记>,在女生面前吹牛热爱&l ...

  9. Backbone实践案例

    By:软件11 王思伦 2013-10-4 Backbone简述: Backbone基于MVC架构,用于开发重量级Javascript应用的框架. 如上文所述,Backbone包含多种类,但主要包含了 ...

  10. 12.23daily_scrum

    今天大家的工作重心在调试过程中,以便及时地发现和解决在调试过程中出现的问题和漏洞,悬浮窗测试工作也已经展开,主要集中在边缘设计代码的测试部分,具体工作如下: 具体工作: 小组成员 今日任务 明日任务 ...