一、视图

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

      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. 20155320《网络对抗》Exp4 恶意代码分析

    20155320<网络对抗>Exp4 恶意代码分析 [系统运行监控] 使用schtasks指令监控系统运行 首先在C盘目录下建立一个netstatlog.bat文件(由于是系统盘,所以从别 ...

  2. Word2010去除灰色中括号标记

    在使用的Word复制内容时,有时会出现这种情况: 去除灰色中括号 出现这种情况,是因为无意中插入了书签.解决方案如下: 或者直接使用ctrl+shift+F5,选择要删除的标签

  3. winform和wpf如何实现鼠标穿透的效果

    先看一下鼠标穿透的效果: 可以看到Form1这个程序虽然遮在了桌面的上面,但是我们还可以在窗体上点击桌面上的必应词典和网易邮箱大师,好像这个叫“Form1”的窗口被“穿透”一样. winform版本: ...

  4. HTML基础之DOM操作

    DOM(Document Object Model 文档对象模型) 一个web页面的展示,是由html标签组合成的一个页面,dom对象实际就是将html标签转换成了一个文档对象.可以通过dom对象中j ...

  5. R绘图 第八篇:绘制饼图(ggplot2)

    geom_bar()函数不仅可以绘制条形图,还能绘制饼图,跟绘制条形图的区别是坐标系不同,绘制饼图使用的坐标系polar,并且设置theta="y": coord_polar(th ...

  6. EF查询百万级数据的性能测试--单表查询

    一.起因  个人还是比较喜欢EF的,毕竟不用写Sql,开发效率高,操作简单,不过总是听人说EF的性能不是很好,也看过别人做的测试,但是看了就以为真的是那样.但是实际上到底是怎么样,说实话我真的不知道. ...

  7. jinkens 构建java及vue 项目

  8. Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)

    目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充.............. ...

  9. 学习 google file system 心得体会

    Google File system文件系统,是在特别便宜的普通硬件设备上运行,它是一个面向大规模数据密集型运用的.可伸缩的分布式文件系统. 与传统文件相比,它认为组件失效是很平常的事件,因为GFS包 ...

  10. Bootstrap 样式设计 栅格系统

    .col-xs- 超小屏幕 手机 (<768px) .col-sm- 小屏幕 平板 (≥768px) .col-md- 中等屏幕 桌面显示器 (≥992px) .col-lg- 大屏幕 大桌面显 ...