数据库DIY第四课

小编 2026-06-05 阅读:232 评论:0
数据库第四课 1.计算机英语单词 distinct 去重 2.实体关系中的多对多的关系 1.多对多的关系建立,创建的第三张关系表,关系表必有的字段为关系标的本身的主键,以及另外两张表的主...

数据库第四课

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;
    

作业

\"\"

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表