指明外键:
1 :1
两个表中的主键都可以当成外键
1 :N
在 "多" 的实体表中新增加一个字段,该字段是 "一" 实体表的主键
M : N
拆成两个1 :N的关系
在 "多" 的实体表中新增加一个字段,该字段是 "一" 实体表的主键、 逻辑模型---->用SQL实现数据库
create database yr; use yr;
在students表中增加两个限制;
1.not null非空限制
2.primary key 主键限制
create table students(
-> s_id int not null primary key,
-> name varchar(20),
-> gender varchar(20),
-> age int,
-> address varchar(50)
-> ); create table teachers(
-> name varchar(20) not null primary key,
-> gender varchar(20),
-> rank varchar(20)
-> );
rank 级别
gender 性别 create table textbooks(
-> ISBN varchar(20) not null primary key,
-> name varchar(20),
-> auther varchar(20),
-> publisher varchar(20)
-> );
author 作者
publisher 出版社
textbook 教材 create table courses(
-> c_id int not null primary key,
-> name varchar(20),
-> credit int,
-> instructor varchar(20),
-> ISBN varchar(20),
-> foreign key(instructor) references teachers(name),
-> foreign key(ISBN) references textbooks(ISBN)
-> );
instructor 授课教师
credit 学分
references 关联
foreign key 外键 create table registration(
-> s_id int not null,
-> c_id int not null,
-> term varchar(20),
-> grade decimal(4,1),
-> primary key(s_id,c_id),
-> foreign key(s_id) references students(s_id),
-> foreign key(c_id) references courses(c_id) ); insert into students values(1,"Ann","F",21,"beijing"); insert into textbooks values("","Intro to SQL","Yang","Renmin Press"); insert into teachers values("Yang","F","Instructor"); insert into courses values("","Databases",3,"Yang",""); insert into registration values(1,1,201711,93.0); insert into students values(2,"Bob","M",23,NULL);
错误的 s_id是主键不能重复
insert into students values(2,"ob","M",23,NULL);
ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY' 提示2重复 注意:SQL中,空值是null 思考题:************
1.查询students表中,地址为空的学生信息
select * from students where address is null;[当为null的时候不能用=必须使用is]
2.查询students表中,地址不为空的学生信息
select * from students where address is not null; 演示foreign key限制错误:
insert into courses values("","python",3,"wei","");[老师的值一定是teacher表中应该有的老师,如果没有报错,这个外键是teachers中的主键] insert into teachers values("Wei","M","Instructor"); insert into textbooks values("","Intro to python","Guido","Beijing Press"); 规范化:
第一范式:
1.消除重复的行和列
2.定义主键,唯一标识所有记录
3.所有其他字段必须直接或间接的依赖于主键
4.所有字段必须只包含一个值
5.每个字段中的所有值必须是相同的数据类型
第二范式:
1.表必须处于1NF中
2.所有非键值必须完全函数依赖于主键
3.当主键为复合键时,必须删除部分依赖 高级SQL:
create table orde(
-> o_id int not null,
-> o_name varchar(50),
-> date datetime,
-> c_id int,
-> amount decimal(10,2)
-> ); insert into orde values(100,"computer","2011-11-1 12:34:56",1,8800);
insert into orde values
(101,"iphone","2011-10-10 15:20:56",3,6600),
(103,"ipad","2017-9-13 09:34:45",4,450),
(104,"pen","2016-10-18 15:20:56",3,6600),
(105,"iphone","2015-11-10 16:20:56",5,6000),
(106,"ipad","2014-09-12 14:20:56",2,10000); joins
利用两个表中共同含有的列的相同值来连接 select 列名 from 第一个表,第二个表
where 第一个表.列名 = 第二个表.列名 select id,name,salary,o_name,amount from customers,orde where customers.id =orde.c_id; select id,name,salary,o_name,amount from customers,orde;[就变成笛卡尔积了就是两个表格的所有组合] 练习;
打印出顾客消费额度超过其工资的人员名单和工资信息以及消费额度
select id,name,salary,amount from customers,orde where (customers.salary < orde.amount)and(customers.id =orde.c_id); 子查询;嵌入到另一个select语句中的select语句 select 列名或者表达式 from 表名 where 表达式 比较操作符{all|any}(子查询语句);[子查询语句一定是select语句]
1.返回单值的子查询
select * from customers where customers.id =(select c_id from orde where o_id=100);
等同于
select c.id,c.name,c.age,c.address,c.salary from customers as c,orde as o where c.id = o.c_id and o.o_id=100; select c.id,c.name,c.age,c.address,c.salary from customers as c[这个是重命名相当于customers=c],orde as o where c.id = o.c_id and o.o_id=100; 2.返回多值的子查询
select * from customers where customers.id in (1,2,3); select * from customers where customers.id in (select c_id from orde where amount>5000);
[顾客的身份的id在所有订单消费大于5000的c_id中 ]
练习:
选出地址在北京或者上海的顾客,该顾客消费7000元
select * from customers where customers.id in (select c_id from orde where amount>7000 ) and(address="beijing" or "shanghai" ); 子查询与聚合函数配套练习:************
选择消费最少的顾客信息
select * from customers where customers.id =(select c_id from orde where amount=(select min(amount) from orde));
select * from customers where customers.id =(select c_id from orde order by amount asc limit 1);
打印出orde表中amount的最小值
select min(amount) from orde;
select c_id from orde where amount=450; 选择工资salary最少的顾客的信息
select * from customers where salary= (select min(salary) from customers); all/any和子查询的用法
select * from customers where age > any(24,25,27);
select * from customers where age > all(24,25,27); insert 语句嵌套子查询:
insert into 表名[列名1,列名2,....列名n] values(value1,value2,....valuen); insert into 表名[列名1,列名2,....列名n] select [列名1,列名2,....列名n] from 表名 [where 条件]; create table customers_1(
-> id int,
-> name varchar(20),
-> age int,
-> address char(25),
-> salary decimal(18,2)
-> ); insert into customers_1 select * from customers where id in (select c_id from orde where amount>8000);
insert into customers_1 select * from customers where id[customers的客户id号] in (select c_id from orde where amount>8000); update[更改]语句的嵌套查询 update 表名 set 列名=新的值 where 列名 操作符(select 列名 from 表名 [where 条件]);
示例:
update customers set salary =salary*1.5 where age in (select age from customers_1 where age>=20); 练习:
1.把customers表中工资小于所有顾客的平均消费的人的工资提高一倍
select * from customers where id in( select c_id from orde where amount= (select avg(amount) from orde) <= customers.salary);
先找出数据然后在如下进行修改
update customers set salary =salary*1.5 where id in( select c_id from orde where amount= (select avg(amount) from orde) <= customers.salary);
update customers set salary =salary*1.5 where salary>=(select avg(amount) from orde);
2.把customers表中的新数据,按照工资由大到小排序[不用where,where是加条件用的]
select * from customers order by salary desc; 3.求出customers表中各个城市的人的平均工资
select address,avg(salary) from customers group by address; 4.求出customers表中消费额度大于全体顾客的平均工资的顾客的全部信息
select * from customers where salary in (salary=select avg(salary) from customers) <(select amount from orde);
select * from customers where salary =(select avg(salary) from customers) <orde.amount; select * from customers where customers.id in (select c_id from orde where amount<(select avg(salary) from orde));
5.列出工资高于平均消费额的客户的总个数
select count(*) from customers where salary>(select avg(amount) from orde);

MySQL的一些基本命令笔记(3)的更多相关文章

  1. MySQL的一些基本命令笔记(4)

    delete 语句嵌套子查询: delete from 表名1 where 列名 操作符 (select 列名 from 表名2 where 条件); 示例: delete from customer ...

  2. MySQL的一些基本命令笔记(1)

    关系型数据库的建模构建块: 1.数据是以行和列的形式存储数据. 2.这一系列的行和列称为表(关系) 3.表中的每一行表示一条记录(元组) 4.表中的每一列表示记录的一个属性 5.一组表组成了数据库 6 ...

  3. MySQL的一些基本命令笔记(2)

    1.逻辑运算符的补充 between 的用法:(在....之间) select column1,column2,......columnN from 表名 where columnX between ...

  4. Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记

    Ubuntu 14 编译安装 PHP 5.4.45 + Nginx  1.8.0/1.4.7 + MySQL 5.6.26 笔记,主要是给自己的PC机安装,非生产环境! 一.下载必要的源码 1.1.下 ...

  5. MYSQL视图的学习笔记

    MYSQL视图的学习笔记,学至Tarena金牌讲师,金色晨曦科技公司技术总监沙利穆 课程笔记的综合. 视图及图形化工具   1.       视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚 ...

  6. mySQl数据库的学习笔记

    mySQl数据库的学习笔记... ------------------ Dos命令--先在记事本中写.然后再粘贴到Dos中去 -------------------------------- mySQ ...

  7. Mysql数据库基础学习笔记

    Mysql数据库基础学习笔记 1.mysql查看当前登录的账户名以及数据库 一.单表查询 1.创建数据库yuzly,创建表fruits 创建表 ) ) ,) NOT NULL,PRIMARY KEY( ...

  8. CentOS6.8下MySQL MHA架构搭建笔记

    转载请注明出处,本文地址:http://www.cnblogs.com/ajiangg/p/6552855.html 以下是CentOS6.8下MySQL MHA架构搭建笔记 IP资源规划: 192. ...

  9. Mysql经常使用基本命令汇总及默认账户权限与改动

    一直仅仅是在浅显利用数据库存储数据.也被windows惯坏了.非常多命令使用的时候记不起来.so,换LINUX系统!不再使用GUI管理数据库!也想深入学习下Mysql.从权限管理開始.也就诞生了这篇学 ...

随机推荐

  1. 推荐一套Angular2的UI模板

    Core UI Core UI是一款基于Bootstrap4的UI模板,有html.angular2,react和vue版.我是在使用angular2版本中发现其项目结构不符合angular风格指南推 ...

  2. Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式

    一.通过InstallUtil.exe安装.卸载.启动.停止Windows Service 方法一 1.以管理员身份运行cmd 2.安装windows服务 切换cd C:\Windows\Micros ...

  3. velocity模板引擎 -- java.io.FileNotFoundException: velocity.log (Permission denied)

    问题原因是velocity的日志框架导致(velocity是使用自己封装的日志框架记录日志的),velocity在初始化Logger时,如果没有读取到配置文件,则会使用默认的velocity.log做 ...

  4. Java 8 中为什么要引出default方法

    (原) default方法是java 8中新引入进的,它充许接口中除了有抽象方法以外,还可以拥用具有实现体的方法,这一点跟jdk8之前的版本已经完全不一样了,为什么要这样做呢? 拿List接口举例,在 ...

  5. 【vue】项目目录结构及使用多的知识点

    项目目录: Node_modules/npm安装的该项目的依赖库 vuex/文件夹存放的是和 Vuex store 相关的东西(state对象,actions,mutations) router/文件 ...

  6. day 9~11 函数

    今日内容 '''函数四个组成部分函数名:保存的是函数的地址,是调用函数的依据函数体:就是执行特定功能的代码块函数返回值:代码块执行的结果反馈函数参数:完成功能需要的条件信息​1.函数的概念2.函数的定 ...

  7. Vim配置(python版)

    由于马上将用到django框架,需要有一个好的ide来coding,之前做C的开发时候体会到了vim的强大,所以编写python也决定采用vim. PS:除了vim,一般浏览代码多用atom和subl ...

  8. 类别不平衡问题和Softmax回归

    目录 类别不平衡(class-imbalance) Softmax回归模型 类别不平衡(class-imbalance) 当不同类别的训练样本数目差别很大,则会对学习过程造成困扰.如有998个反例,但 ...

  9. 【原创】架构师必备,带你弄清混乱的JAVA日志体系!

    引言 还在为弄不清commons-logging-xx.jar.log4j-xx.jar.sl4j-api-xx.jar等日志框架之间复杂的关系而感到烦恼吗? 还在为如何统一系统的日志输出而感到不知所 ...

  10. Entity Framework Core系列之DbContext

    前言: EF Core DbContext表示与数据库的会话,并提供与数据库通信的API,具有以下功能: 数据库连接 数据操作,如查询和持久化 更改追踪 模型构建 数据映射 对象缓存 事务管理 数据库 ...