Mysql 约束和基础查询

161 阅读5分钟

约束

约束的作用:限制数据的插入和删除
约束分为:主键约束,非空约束,唯一约束,默认约束

主键约束

关键字:primary key
特点:不能重复,不能为空
注意:一个表只能有一个主键
主键可以设置自增,设置自增的字段必须是整数类型,关键字:auto_increment
特点:每次加1,可以使用null和0占位
# 添加主键约束
create table stu2(
    id int primary key ,
    name varchar(100),
    age int
);
# 查看表结构
desc stu2;
# 演示主键约束的特点: 限制对应数据不能重复不能为空
insert into stu2(id, name, age) VALUES (1,'张三',18); # 成功
insert into stu2(id, name, age) VALUES (1,'张三',18); # 报错,因为数据重复
insert into stu2(id, name, age) VALUES (2,'李四',28);
# 演示如何删除主键
alter table stu1 drop primary key;

image.png

非空约束

关键字:not null
特点:可以重复,不能为空
注意:一个表可以有多个非空约束
# 创建表
create table stu4(
    id int not null,
    name varchar(100) not null,
    age int
);

# 查看表结构
desc stu4;

# 演示非空约束特点: 限制数据不能为空null
insert into stu4 (id, name, age) VALUES (1,'张三',18); # 成功
insert into stu4 (id, name, age) VALUES (null,null,null); # 报错,因为id和name不能为空

# 了解建表后添加和删除非空约束
# 添加非空约束
alter table stu4 change age age int not null;
# 删除非空约束
alter table stu4 change age age int;

唯一约束

唯一约束特点: 限制对应的数据不能重复
注意: 一个表中可以有多个唯一约束
# 创建表
create table stu5(
    id int not null unique ,
    name varchar(100) unique ,
    age int
);
# 查看表结构
desc stu5;
# 演示唯一约束特点: 限制数据不能重复
insert into stu5 (id, name, age) VALUES (1,'张三',18); # 成功
insert into stu5 (id, name, age) VALUES (1,'李四',28); # 报错,因为id不能重复
insert into stu5 (id, name, age) VALUES (null,'王五',null); # 报错,因为id设置唯一约束同时也设置了非空约束

# 了解建表后添加和删除唯一约束
# 添加唯一约束(自动添加唯一索引,索引名就是字段名)
alter table stu5 change age age int unique ;
# 删除唯一约束(本质就是删除唯一索引)
drop index age on stu5;

# 添加唯一索引(手动添加唯一索引,自定义索引名称)
create unique index age_index on stu5 (age);
# 删除唯一约束(本质就是删除唯一索引)
drop index age_index on stu5;

默认约束

关键字:default
特点:给字段设置默认值,当插入数据时该字段没有设置值时,使用默认值,有时使用插入的值(就近原则)
注意:一个表可以有多个默认值
# 创建表
create table stu6(
    id int primary key auto_increment,
    name varchar(100) default 'admin',
    pwd varchar(100) default '123456'
);
# 查看表结构
desc stu6;

# 演示默认约束特点: 如果用户没有指定字段值,可以使用提前设置好的默认值
insert into stu6(id) values (1);
insert into stu6(name) values ('张三');
insert into stu6(name,pwd) values ('李四','abcde');

# 了解建表后添加和删除默认约束
create table stu7(
    id int primary key auto_increment,
    name varchar(100) ,
    pwd varchar(100)
);
# 添加默认约束
alter table stu7 change name name varchar(100) default 'admin';
alter table stu7 change pwd pwd varchar(100) default '123456';
# 删除默认约束
alter table stu7 change name name varchar(100);
alter table stu7 change pwd pwd varchar(100);

简单查询

基础查询

关键词:select from
格式:select 字段名 from 表名;
去重功能:select distinct 字段名 from 表名;
# 1.数据准备
use day02_db;
# 创建商品表
CREATE TABLE product
(
    pid         INT PRIMARY KEY,
    pname       VARCHAR(20),
    price       DOUBLE,
    category_id VARCHAR(32)
);
# 插入数据
INSERT INTO product(pid,pname,price,category_id) VALUES(1,'联想',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(2,'海尔',3000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(3,'雷神',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(4,'杰克琼斯',800,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(5,'真维斯',200,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(6,'花花公子',440,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(7,'劲霸',2000,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(8,'香奈儿',800,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(9,'相宜本草',200,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(10,'面霸',5,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(11,'好想你枣',56,'c004');
INSERT INTO product(pid,pname,price,category_id) VALUES(12,'香飘飘奶茶',1,'c005');
INSERT INTO product(pid,pname,price,category_id) VALUES(13,'海澜之家',1,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(14,'小米',1999,'');
INSERT INTO product(pid,pname,price,category_id) VALUES(15,'华为',6999,'null');
INSERT INTO product(pid,pname,price,category_id) VALUES(16,'蜜雪冰城',1,null);
# 需求1: 查询所有的商品信息
select pid,pname,price,category_id from product;
select * from product;

# 需求2: 只查询商品名称和对应价格
select pname,price from product;

# 需求3: 查询商品分类id,要求去重
select distinct category_id from product;

# 需求4: 查询商品名称和对应价格,要求最终展示字段名为商品名称和商品价格
# 实际工作中不建议用中文,此处仅仅为了演示
select pname as 商品名称,price as 商品价格 from product;
# 注意: as 关键字可以省略
select pname 商品名称,price 商品价格 from product;

# 需求5: 所有的商品价格要求加100元展示
select pname 商品名称,price+100 商品价格 from product;

条件查询

条件查询关键字: where

条件查询基础格式: select 字段名 from 表名 where 条件;

	      比较运算符: >  <  >=  <=  !=  <>
	      
	      逻辑运算符: and  or  not
	      
	      范围 查询: 连续范围:between x and y       非连续范围: in(x,y)
	      
	      模糊 查询: 关键字:like   %:0个或者多个字符   _:一个字符
	      
	      非空 判断: 为空: is null    不为空:is not null

条件查询基础格式: select 字段名 from 表名 where 条件;

		      比较运算符: >  <  >=  <=  !=  <>
		      
		      逻辑运算符: and  or  not
		      
		      范围 查询: 连续范围:between x and y       非连续范围: in(x,y)
		      
		      模糊 查询: 关键字:like   %:0个或者多个字符   _:一个字符
		      
		      非空 判断: 为空: is null    不为空:is not null