day02、mysql应用

DDL增强
1、约束
在这里插入图片描述

● 主键
○ 第一种,创建表语句时,添加主键约束
方式一

create table person1(
    id int ,
    name varchar(100),
    income decimal(18,2),
    primary key (id) 
);

方式二

create table person2(
  id int primary key,
  name varchar(100) ,
  income decimal(18,2)
);

○ 第二种: 创建表完成之后,通过alter添加主键约束
语法 : alter table 表名 add primary key(列名,列名…);
● 主键自增
○ 自增列只能是主键,一个表中自增类只能有一个
○ 第一种: 建表时,添加自增

create table person4(
	id int auto_increment ,
	name varchar(200),
	 primary key(id)
);

○ 第二种: 创建表之后,添加自增
语法 : alter table 表名modify 主键列名 类型 auto_increment;
○ 设置自增的起始值
○ 语法: alter table 表名auto_increment=值;
● 外键 外键列的值,必须是关联表中的已有主键值,也可以为空
○ 第一种: 创建表时添加外键约束,注意: 引用student中添加外键列,指向teacher表,所以必须先创建teacher表才行

create table teacher(
    id int ,
    name varchar(20),
    primary key (id)
);
create table student (
    id int ,
    name varchar(20),
    teacher_id int ,
    primary key (id),
    foreign key (teacher_id) references teacher(id)
);

○ 第二种: 创建完表之后,添加外键约束
语法 : alter table 表名 add foreign key (外键列列名) references 指向的表名 (主键列列名);
○ 唯一约束unique
■ 唯一约束是指定table的列或列组合不能重复,保证数据的唯一性。
■ 唯一约束不允许出现重复的值,但是可以为多个null.
■ 第一种: 创建表时,添加unique约束

 create table temp (
  id int ,
  `name` varchar(20),
  unique(id)
);create table temp (
  id int  unique ,
  `name` varchar(20)
);
■ 第二种: 创建表之后,添加unique约束
create table temp1 (
    id int ,
    `name` varchar(20)
);

alter table temp1 add unique (id);
● 非空约束 not null与 默认值 default
○ not null经常和default一起使用
○ 第一种: 创建表时,添加约束

create table temp2(
    id int not null,
    `name` varchar(30) default  'abc',
	sex varchar(10) not null default '男'
);

○ 测试:只添加id name和性别会有默认值
○ 第二种: 创建表之后,添加约束
alter table temp3 modify id int not null ;
alter table temp3 modify name varchar(30) default ‘abc’;
alter table temp3 modify sex varchar(10) not null default ‘男’;
○ 测试: 只添加id,name和sex都有默认值
2、条件判断
● and 优先级大于or,相当于&&
select 列限定 from 表限定 where A表达式 and B表达式;
● or 相当于||
select 列限定 from 表限定 where A表达式 or B表达式;
● =
● <>
● 注意: = 和 <> 额外留意,和java中有所不同,java中判断相等用 == , 这里只用 = , java中判断不相等用 != , 这里使用 <>
● is (not) null 不为是null或不为null
● between and
○ 值1到值2的范围
select 列限定 from 表限定 where 列名 between 值1 and 值2;
● in(97,99)
○ 在指定数据中,是具体的值
select 列限定 from 表限定 where 列名 in(值1,值2…);
● like模糊查询
○ %:匹配任意个数字符
○ _:匹配单个字符
select 列限定 from 表限定 where 列名 like ‘%值%’ ;
● order by 排序
○ 默认升序
select 列限定 from 表限定 order by 列名 asc/desc;
Asc : 升序
Desc : 降序
● limit 限制条数,通常和order by一起使用,因为我们使用排序之后,再去获取前几条数据,比较有价值,比如成绩前三名
○ limit 3:取前三个
○ limit 10,10:从第11个开始取取10个
select 列限定 from 表限定 limit 条数;
select 列限定 from 表限定 limit 开始值(不包含) ,条数;
组函数
● count()总条数
● max (字段名) 最大值
● min(字段名)最小值
● sum(字段名)求和
● avg(字段名)平均值
● group by 分组
● having筛选 select teacher_id, avg(score) as avg from student group by teacher_id having avg > 60;
select count(
),max(字段名),min(字段名)… from 表名 group by 字段名;
● 案例
查询每个老师分别带了多少学生(显示老师id即可)
select teacher_id, count(*) as stu_count from student group by teacher_id;
● where书写顺序:from where 行限定 group by having order by limit
● union和常用函数
○ 前后查询的列数相同
○ 前后查询的类型最好一致
○ 前后查询的顺序最好一致
○ union合并查询会去重
○ union all 不去重
○ 语法
select * from student where teacher_id=1
union
select * from student where score > 60;
○ 常用函数
■ select version() ;显示当前MySQL软件的版本
■ select database();显示当前所处数据库是哪个
■ select char_length(‘中国’);返回字符个数。
■ select length(‘中国’);返回字符所占字节数,MySQL中,一个UTF8编码的汉字占3个字节
■ select concat( ‘a’, ‘b’, ‘c’, ‘d’);返回 ‘abcd’。字符串拼接函数
■ select concat_ws( ‘=’, ‘a’, ‘b’, ‘c’);返回 ‘a=b=c’。字符串拼接函数,第一个是拼接间隔符
■ select upper(‘abcd’);返回ABCD。将参数中所有小写字母转换为大写
■ select lower(‘ABCD’);返回abcd。将参数中所有大写字母转换为小写
■ select substring( ‘系统信息类’, 1, 3 );返回 系统信。第2个参数代表从1开始的第几个字符,第3个参数代表截取字符个数
■ select now();返回当前日期时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值