数据库DIY第四课

数据库第四课

1.计算机英语单词

distinct 去重

2.实体关系中的多对多的关系

  • 1.多对多的关系建立,创建的第三张关系表,关系表必有的字段为关系标的本身的主键,以及另外两张表的主键作为关系表的两个外键。

      create table teacher(       
      	id int auto_increment,
      	name varchar(19),primary key(id)
      )auto_increment = 1000;
      
    
      create table grade(       
      	id int auto_increment,
      	name varchar(19),primary key(id)
      )auto_increment = 1000;
    
      create table teacher_grade(
      	id int auto_increment,
      	teacher_id int,
      	grade_id int,
      	primary key(id),
      	foreign key (teacher_id)references teacher(id),
      	foreign key (grade_id)references grade(id)
      );
    
  • 2.表创建好了后怎么样插入数据,就是在两个表分别插入各自的数据,然后在带三个表中插入两两个表对应的ID合成对应插入到第三个表内;

3.项目目标的创建表语句

创建用户表的好友关系是用户与用户之间的多对多的关系
create table mc_user(
	id int not null auto_increment,username varchar(16) not null,
	password varchar(16) not null,nick_name varchar(32),
	gender char(6),age int,register_time datetime,primary key(id)
);
创建mc_friend表
create table mc_friend(
	id int not null auto_increment, user_id int not null,friend_id int not null,primary key(id),
	foreign key (user_id)references mc_user(id),
	foreign key (friend_id)references mc_friend(id)
);

4.导入文件init

5.查询语句整体和个别单独查询

	select * from  表名;
	select 字段名,字段名 from 表名;

6.条件语句where 子句;在select 语句中,where 子句用来赛选查询结果

	select *from teacher where 条件;
	select *from teacher where course='语文';
	数字类型处理结果
	查询student 表中的height是170 的记录;
	where=170;where='170';单引号可以加可以不加都可以;

7.字符、日期类型处理----字符和时间类型的都要用单引号引起来;

	select *from teacher where course='语文';
	select *from teacher where birthday='1999-10-10';

8.查询条件

=	>	<	>=	<=	!=	<>

9.between … and…用来查询符合某个值范围的数据

	select * from teacher where working_years between 5 and 10;

10.and 和 or

  • 1.要同时满足多个条件,使用and逻辑操作符连接这些条件

  • 2.要满足多个条件中一个,使用or逻辑操作连接这些条件

      select * from teacher where working_years>10 and course='数学';
      select * from teacher where city_work='北京' or city_work='上海';
    

11.int 和not in

  • 1.in(值1,值2,值3) not in(值1,值2,值3)

  • 2.in:符合列表中的记录

  • 3.not in:不符合值列表中的记录

      例子:
      查询科目等于数学或者等于语文的所以内容
      select * from tracher where course in('数学','语文');
    

12.like模糊查询

  • 1.like:通过部分信息进行查询

  • 2.select * from 表名 where 列名 like pattern;

  • % :匹配0到多个字符

  • _:匹配1个字符

      例子:
      查询李开头
      select * from teacher where name like '李%';
      查询李子结尾开头
      select * from teacher where name like '%李';
      查询包含李字
      select * from teacher where name like '%李%';
      查询第二个字符为李  __是两个下划线不能有空格
      select * from teacher where name like '__李';
    

13.distinct 过滤重复

	过滤重复的记录
	select city_work from teacher;
	select distinct city_work from teacher;

14.is null 和 is not null

  • 1.is null 为是空的条件

  • 2.is not null 为是不为空的条件;

      例子:
      select * from teacher where city_work is not null;
    

作业

数据库DIY第四课