Mysql Day02(简单应用)

DDL基础与扩展

Alter:

更改表名

# 更改表名
ALTER TABLE  test01 RENAME test02;

# 更改字段名
# name:原来的字段名 newname:新的字段名 数据类型
ALTER TABLE test02 CHANGE name newname VARCHAR(255);

# 添加字段
# 字段名 数据类型
ALTER TABLE test02 ADD sex VARCHAR(255);

# 删除字段
ALTER TABLE test02 DROP sex;

# 更改字段类型(尽量不要改)
ALTER TABLE test02 MODIFY id VARCHAR(255);

# 修改数据库字符集(西药重启mysql服务才能生效)
ALTER DATABASE test CHARACTER SET utf8;

# 修改表字符集
ALTER TABLE test02 CHARACTER SET utf8 COLLATE utf8_general_ci;

DDL增强

约束的分类

在这里插入图片描述

主键

主键通常用于唯一确定表中的一条记录,设置为主键的字段是不能为NULL并且不能重复的。

# 第一种 创建表语句的时候添加主键约束
CREATE TABLE person1(
id int,
name varchar(255),
income decimal(18,2),
primary key(id,name)
);
# 只有一个主键的时候也可以直接添加
CREATE TABLE person2(
id int primary key,
name varchar(255),
income decimal(18,2)
);
# 第二种 创建表之后使用alter添加主键约束
CREATE TABLE person3(
id int,
name varchar(255),
income decimal(18,2),
);
alter table person3 add primary key (id);
主键自增(auto_increment)
# 第一种,建表时添加自增
create table person4(
id int auto_increment,
name varchar(200),
primary key(id)
);
# 第二种,创建表之后,添加自增
create table person5(
id int,
name varchar(200),
primary key(id)
);
#  alter table 表名 modify 主键列名 类型 auto_increment;
alter table person5 modify id int auto_increment; 
# 设置自增的值
#  alter table 表名auto_increment=值; 
alter table person5 auto_increment=100;
外键

通过这个表中的外键,去找另一个表的主键,能找到主键就能根据主键找到对应的一行数据
常用于有关联关系的两个表中
外键列的值,必须是关联表中的已有主键值,也可以为空

# 第一种,创建表的时候就添加
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)
);

注意 : 引用student中添加外键列,指向teacher表,所以必须先创建teacher表才行

# 第二种,建表之后添加外键约束
create table student1 (
    id int ,
    name varchar(20),
    teacher_id int,
    primary key (id)
);
create table teacher1(
    id int ,
    name varchar(20),
    primary key (id)
);
# alter table 表名 add foreign key (外键列列名) references 指向的表名 (主键列列名);
alter table student1 add foreign key (teacher_id) references teacher1(id);

唯一: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)

所有的类型的值都可以是null,包括int、float 等数据类型,设置为not null的字段,必须填入数据
经常和default一起使用,当不填写数据的时候,把默认值设置成指定的值

#建表时添加约束
create table temp2(
    id int not null,
    `name` varchar(30) default  'abc',
	sex varchar(10) not null default '男'
);

#建表后添加约束
create table temp3(
    id int,
    `name` varchar(30) ,
	sex varchar(10) 
);
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 '男';

条件判断

and:和

or:或

> , >= , < , <= ,<>,=
> : 大于
< : 小于
>= : 大于等于
<= : 小于等于
= : 相等
<> : 不等于
注意 : = 和 <> 额外留意,和java中有所不同,java中判断相等用 == , 这里只用 = , java中判断不相等用 != , 这里使用 <>

between a and b:介于a和b之间,包括a和b

模糊查询 like

%匹配任意个数的任意字符
_匹配单个任意字符
如想查询_和%需要使用转义符\

order by 排序

select 列限定 from 表限定 order by 列名 asc/desc;
Asc : 升序
Desc : 降序

limit

限制条数,通常和order by一起使用
语法 :
select 列限定 from 表限定 limit 条数;
select 列限定 from 表限定 limit 开始值(不包含) ,条数;

单表查询(组函数)

常用组函数:
count(*) : 总条数

max(字段名) : 最大值

min(字段名) : 最小值

avg(字段名) : 平均值

sum(字段名) : 总和

group by

分组

having

过滤

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值