Mysql(三)约束

1、视图java

      视图是虚拟的数据表,自己不存储数据,而是提供数据的逻辑 展现。数据库

      一、建立视图oop

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
      二、修改视图spa

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

  三、删除视图rest

drop view stu_view;对象

       四、 在修改视图时,咱们能够不使用alter,而是or replace。 在视图不存在时,建立视图,当视图存在时,替换视图。blog

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;索引

       五、视图只是一个预存储的查询语句。(as后面的查询语句)  当咱们查询视图时,视图的查询语句就会展开。(就是从 视图存储的查询语句(结果集)中查询。io

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 视图能够提供相同数据表的不一样逻辑展现。

 

2、

    视图默认状况下,是以咱们查询的字段命名。
#当视图的查询字段出现同名时(命名冲突)时,咱们能够:
#1 使用别名 当使用别名时,视图字段会以别名来命名。
#2 自定义视图字段的名称

     一、别名

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;

 二、自定义视图字段的名称,指定的字段数量与查询的字段数量必须一致。

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;

 

3、索引

      索引是数据表中一个特殊的对象。

优势:索引能够加快记录的查询速度。

缺点:
#1 索引须要占用额外的硬盘空间。
#2 当数据表中记录发生变更时(增长,修改等),索引须要 进行从新变动(从新维护)。

索引使用的场合:

1 数据量很大(表中的记录多)
2 查询操做多
3 增长,删除等操做少。

      一、建立索引

create index stu_index on student(id);

create index 索引名 on 表名(字段1,字段2……字段n);     能够指定一列或者多列

      二、删除索引

drop index 索引名 on 表名

 

drop index stu_index on student

索引这就相似于图书的目录同样,若是咱们想要查询某些内容, 咱们能够从第一页开始,逐页进行查找,但这势必会耗费大 量的查找时间。可是,若是咱们经过目录来查询,就能够快 速的定位到相关的页码上。 
 
4、数据库约束
  

约束
约束,就是一种限制,其能够保证数据的正确性与完整性。
约束能够分为以下几类:
1惟一性约束
2非空约束
3主键约束
4外键约束
5检查约束

约束具备字段级语法与表级语法。

一、惟一性约束

惟一性约束保证约束列(一列或多列)的值不能出现重复。惟一性约束容许加入多个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;

二、非空约束

非空约束表示字段不容许为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;

三、主键约束

主键字段既不能为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;

四、外键约束:

若是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

   五、检查约束(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 字段级语法不能为约束命名,而表级语法能够为约束命名。

 

5、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;

6、子查询

子查询即查询中还有查询(嵌套查询)
根据子查询出现的位置,能够将子查询分为两类:
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 stayselect 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)

相关文章
相关标签/搜索