PYTHON3.Mysql_expert.day03

1. Foreign Key
     1. Create the foreign key table
         the Create the Table xxxx (
             xxx xxxx,
             constraint A foreign key name of the foreign key (field)
             the References master table (primary key)
             ON the Delete Cascade | the restrict | null the SET
             ON Cascade Update | the restrict | the SET null
         )
     2. modify the foreign key table to increase
         the ALTER the table xxx
         the Add constraint A foreign key name of the
         foreign key (field)
         the References master table (primary key)
         ON the Delete Cascade | the restrict | null the SET
         ON Cascade Update | the restrict | the SET null
     3. Check the foreign key
         show create table XXXX
     4. remove the foreign key
         ALTER Table XXXX
         drop foreign key name foreign key
2. join query
     1. The cross-connect
         select * from A, B where A.bid = B.id
     connections within 2
         to join two tables, the data satisfying the condition screened
         SELECT * from A
         Inner the Join B
         ON = A.bid B.id
= ================================================== ====
1. join query
     1. an outer connector
         1. left outer connector
             1. role
                 1. the left table inquiries from all data (even if the condition is not satisfied)
                 2. the right table satisfies the condition data associated check out
                 3 is not associated with the data fields will be associated with a null filling
             2. syntax
                 select fields
                 from the Join B a left
                 oN condition associated

       

--1 left outer connection: the left table teacher, the right table course, related conditions: teacher.course_id = course.id
select * from
teacher left join course
on teacher.course_id = course.id

- 2 left outer connection: the left table course, the right table teacher, associated conditions: teacher.course_id = course.id
select * from
course left join teacher
on teacher.course_id = course.id


                Left outer join

2. The right outer connector
             1. Role
                 1. right table inquiries from all data
                 2. The left table satisfies conditions associated data inquiries from
                 3 associated with the data associated with the field is not null, as will be filled
             2. Syntax
                 select field
                 from the Join A right B
                 ON condition associated

   

- 3. The right outer join, the left table: teacher, the right table: course, related conditions: teacher.course_id = course.id
select * from
teacher right join course
on teacher.course_id = course.id;


select * from
course right join teacher
on teacher.course_id = course.id;

The right connection
         3. Full male connector
             1. Role
                 the two tables do data associated with the query, the association is obtained on the normal display
                 associated not on the null-filling places
             2. Syntax
                 SELECT * from
                 A Full the Join B
                 ON condition associated

     

- 4. Full outer connection (error)
select * from
course full join teacher
on full.id = teacher.course_id

- 5. Review the information did not participate in the examination of students
select * from
student left join score
on student.id = score.stu_id
where score.score is null;

Exercise 1
2. subquery
     1. What is a sub-query
         results to a query as a condition outside of the operation appears
     2. grammar
         select .... from table where condition = (the SELECT ...)
         select .... from (inquiry )

     exercise 3: query student table than the 'John Doe' older student information
         select * from student where age> (Joe Smith');

Exercise 3
    Exercise 4:

        1.查询考过"齐天大圣"老师所教课程的学员的信息
         2.查询在score表中有成绩的学员的信息
         3.查询"Python基础"课程并且分数在80分以上的
           学员的姓名和毕业院校
         4.查询和"张三"相同班级以及相同专业的同学的信息

    

-- 练习4
-- 1.查询考过齐天大圣老师所教授课程的学员信息
-- 1.1查询该老师所教课程的id
select course_id from teacher where name='齐天大圣';
-- 1.2从course 表中查询出course_id为1的stu_id的值
select stu_id from score where course_id = (select course_id from teacher where name='齐天大圣');
-- 1.3 从student 中查出id在以上查询结果中出现的学员信息
select * from student where id in (
  select stu_id from score where course_id = (
    select course_id from teacher where name='齐天大圣'
  )
);
-- 2.查询在score表中有成绩的学员信息
 select * from student where id in(select stu_id from score);
-- 3.查询‘python基础’课程并且分数在820以上的学员姓名和毕业院校
select name,school from student
  where id in(select stu_id from score
  where course_id = (select id from course where cname = 'python基础') and score > 80
  );
-- 4.查询和‘张三’相同班级及相同专业的同学信息
select * from student where name != '张三' and class_id = (select class_id from student where name = '张三')
and major_id = (select major_id from student where name = '张三');


3.E-R模型
     1.什么E-R模型
         Entity - Relationship 模型 (实体-关系模型)
         在数据库设计阶段一定会使用到
         以图形的方式展示数据库中的表以及表关系
     2.概念
         1.实体 - Entity
             表示数据库中的一个表
             图形表示:矩形框
         2.属性
             表示某实体中的某一特性,即表的字段
             图形表示:椭圆形
         3.关系 - Relationship
             表示实体与实体之间的关联关系
             1.一对一关系(1:1)
                 A表中的一条记录只能关联到B表中的一条记录上
                 B表中的一条记录只能关联到A表中的一条记录上

                在数据库中的实现手段
                 在任意的一张表中增加:
                     1.外键,并引用自另一张表主键
                     2.唯一索引/约束
             2.一对多关系(1:M)
                 A表中的一条记录能够关联到B表中的多条记录
                 B表中的一条记录只能关联到A表中的一条记录

                在数据库中的实现手段
                 在"多"表中增加:
                     1.外键,引用"一"表的主键
             3.多对多关系(M:N)
                 A表中的一条记录能够关联到B表中的多条记录
                 B表中的一条记录能够关联到A表中的多条记录

                在数据库中的实现手段
                 靠第三张关联表,来实现多对多
                     1.创建第三张表
                     2.一个主键,俩外键
                         外键分别引用自关联的两张表的主键

      

-- 1.创建wife表,目的是实现与teacher的一对一关系
-- wife增加外键
create table wife(
id int primary key auto_increment,
name varchar(20) not null,
age int not null,
teacher_id int not null,
constraint fk_teacher_wife foreign key(teacher_id) references teacher(id),
unique (teacher_id)
);

insert into wife(name,age,teacher_id) values('祁夫人',23,1),('吕夫人',76,2);


create table goods(
id int primary key auto_increment,
gname varchar(32) not null,
gprice int not null
);

-- 插入数据
insert into goods(gname,gprice) VALUES
('iphone56',18888),
('ipad43mini',8888),
('华为mate3000',3000);

-- 创建shoppingchart,teacher与goods对对多之间的第三张关联表
create table shoppingcart(
  id int PRIMARY key auto_increment,
  t_id int,
  g_id int,
  count int DEFAULT 1,
  constraint fk_goods_shoppingcart foreign key(g_id) REFERENCES goods(id),
  constraint fk_teacher_shoppingcart foreign key(t_id) REFERENCES teacher(id)
);

-- 测试
insert into shoppingcart(t_id,g_id)VALUES (1,1),(1,2);
insert into shoppingcart VALUES (null,1,3,15),(null,2,2,8),(null,2,3,1);


4.SQL statement optimization
    1. index: frequently select, where, order by fields should be indexed
     2. Finally, add a single query LIMIT 1, stop a full table scan
     3.where clause = try not to use, or give up the index! full table scan
     4. avoid null value determination, before giving up a full table scan index
     5. avoided or connection conditions, or give up a full table scan index
     6. Fuzzy queries avoid using pre%, or a full table scan
     7. avoided use in and not in, otherwise a full table scan
     8. try to avoid using select *, * instead of using a specific field, not to return any of the fields with less than

select * from A inner join B
on A.bid = B.id


For MySQL Navicat
Power Designer - Database Modeling
Microsoft Visio - ER diagram

Guess you like

Origin www.cnblogs.com/shengjia/p/11165778.html