活动地址:CSDN21天学习挑战赛
作者简介:大家好我是狂暴于涛侠本侠
🦸个人主页:狂暴于涛侠
目录
🎇1.MySQL数据库基本操作-DML
🎡基本介绍
DML是指数据操作语言,英文全称是Data Manipulation Language,用来对数据库中表的数据记录进行更新。
关键字:
插入insert
🎨1、数据插入
语法:
insert into 表 (列名1,列名2,列名3…) values (值1,值2,值3…); //向表中插入某些
insert into 表 values (值1,值2,值3…); //向表中插入所有列
-- 数据插入
-- 格式1:insert into 表 (列名1,列名2,列名3...) values (值1,值2,值3...); //向表中插入某些
insert into student(id,name,sex,age,address,tel)
values(1001,'张三','男',1,'北京',1874);
insert into student(id,name,sex,age,address,tel)
values(1002,'张三','男',2,'北京',1774),
(1003,'张三','男',3,'北京',1994),
(1004,'张三','男',4,'北京',1854);
insert into student(id,name)
values(1005,'张三');
-- 格式2:insert into 表 values (值1,值2,值3...); //向表中插入所有列
insert into student values(1006,'张三','男',5,'北京',1854);
insert into student values(1007,'张三','男',6,'北京',1854),
(1008,'张三','男',7,'北京',1854);
🎨2、数据修改
语法:
update 表名 set 字段名=值,字段名=值…;
update 表名 set 字段名=值,字段名=值… where 条件;
-- 格式1:update 表名 set 字段名=值,字段名=值...;
-- 格式2:update 表名 set 字段名=值,字段名=值... where 条件;
-- 将所有学生的地址修改为重庆
update student set address = '重庆';
-- 讲id为1004的学生的地址修改为北京
update student set address = '北京' where id = 1004 ;
-- 讲id<1004的学生的地址修改为北京
update student set address = '北京' where id < 1004;
-- 讲id为1005的学生的地址修改为广州,成绩修成绩修改为100
update student set address = '广州',score=100 where id = 1005;
-- 将张三的id在原有基础上加1000
update student set id = id + 1000 where name = '张三' ;
🎨3、数据删除
语法:
delete from 表名 [where 条件];
truncate table 表名 或者 truncate 表名
-- 删除数据
-- 格式:delete from 表名 [where 条件];
-- truncate table 表名 或者 truncate 表名
-- 1.删除sid为1004的学生数据
delete from student where sid = 1004;
-- 2.删除表所有数据
delete from student;
-- 3.清空表数据
truncate table student;
truncate student;
注意:delete和truncate原理不同,delete只删除内容,而truncate类似于drop table ,可以理解为是将整个表删除,然后再创建该表;
以下是思维导图:
🎇2.MySQL约束
- 概念
约束英文:constraint
约束实际上就是表中数据的限制条件 - 作用
表在设计的时候加入约束的目的就是为了保证表中的记录完整性和有效性,如用户表有些列的值(手机号)不能为空,有些列的值(身份证号)不能重复。 - 分类
主键约束(primary key) PK
自增长约束(auto_increment)
非空约束(not null)
唯一性约束(unique)
默认约束(default)
零填充约束(zerofill)
外键约束(foreign key) FK
🎡1、MySQL约束-主键约束
-
概念
- MySQL主键约束是一个列或者多个列的组合,其值能唯一地标识表中的每一行,方便在RDBMS中尽快的找到某一行。
- 主键约束相当于 唯一约束 + 非空约束 的组合,主键约束列不允许重复,也不允许出现空值。
- 每个表最多只允许一个主键
- 主键约束的关键字是:primary key
- 当创建主键的约束时,系统默认会在所在的列和列组合上建立对应的唯一索引。
-
操作
- 添加单列主键
- 添加多列联合主键
- 删除主键
🧨操作-添加单列主键
创建单列主键有两种方式,一种是在定义字段的同时指定主键,一种是定义完字段之后指定主键
方式一 语法:
– 在 create table 语句中,通过 PRIMARY KEY 关键字来指定主键。
–在定义字段的同时指定主键,语法格式如下:
create table 表名(
…
<字段名> <数据类型> primary key
…
)
use mysql1;
create table emp1(
eid int primary key,
name VARCHAR(20),
deptId int,
salaey double
);
方式二 语法:
–在定义字段之后再指定主键,语法格式如下:
create table 表名(
…
[constraint <约束名>] primary key [字段名]
);
create table emp2(
eid INT,
name VARCHAR(20),
deptId INT,
salary double,
constraint pk1 primary key(id) --constraint pk1 可以省略
);
主键的特点
-- 主键约束相当于 唯一约束 + 非空约束 的组合,主键约束列不允许重复,也不允许出现空值。
-- 下面代码运行不了 主键为空报错
insert into emp1(eid,name,deptId,salaey) values(,'张三',10,5000);
-- 下面这个代码没错
insert into emp1(eid,name,deptId,salaey) values(1000,'张三',10,5000);
-- 当运行完上述代码下面的运行不了,因为主键重复不可以
insert into emp1(eid,name,deptId,salaey) values(1000,'李四',10,5000);
🧨操作-添加多列主键(联合主键)
所谓的联合主键,就是这个主键是由一张表中多个字段组成的。
注意:
1. 当主键是由多个字段组成时,不能直接在字段名后面声明主键约束。
2. 一张表只能有一个主键,联合主键也是一个主键
语法:
create table 表名(
…
primary key (字段1,字段2,…,字段n)
);
-- 联合主键
-- 所谓联合主键,就是这个主键是由一张表中的多个字段组成的。
-- primary key(字段1,字段2,字段n...)
create table emp3(
name varchar(23),
deptId int,
salary double,
CONSTRAINT pk2 primary key(name,deptId)
);
-- 联合主键特点
-- 以下代码都可实现
-- 联合主键只要联合的主键不是完全一样就可以创建
insert into emp3 values('李四',10,2000);
insert into emp3 values('王五',10,2000);
insert into emp3 values('李四',20,2000);
-- 以下代码都不可实现
-- 联合的主键任意一个为空都是不可以的
insert into emp3 values('李四',null,2000);
insert into emp3 values(null,10,2000);
insert into emp3 values(null,null,2000);
🧨通过修改表结构添加主键
主键约束不仅可以在创建表的同时创建,也可以在修改表时添加。
语法:
create table 表名(
…
);
alter table <表名> add primary key(字段列表);
-- 添加单列主键
create table emp4(
eid int,
name varchar(20),
deptId int,
salary double
);
alter table emp4 add primary key(eid);
-- 添加多列主键
create table emp5(
eid int,
name varchar(20),
deptId int,
salary double
);
alter table emp5 add primary key(name, deptId);
🧨删除主键约束
一个表中不需要主键约束时,就需要从表中将其删除。删除主键约束的方法要比创建主键约束容易的多。
语法:
–不需要指定删除那列,因为每张表就一个主键
alter table <数据表名> drop primary key;
– 删除单列主键
alter table emp1 drop primary key;
– 删除联合主键
alter table emp5 drop primary key;
-- 删除主键
-- 1.删除单列主键
alter table emp1 drop primary key;
-- 2.删除多列主键
alter table emp5 drop primary key;
🎡3、MySQL约束-自增长约束
- 概念
在 MySQL 中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值。每增加一条记录,主键会自动以相同的步长进行增长。
通过给字段添加 auto_increment 属性来实现主键自增长
语法:
字段名 数据类型 primary key auto_increment
操作
create table t_user1(
id int primary key auto_increment,
name varchar(20)
);
-- 自增正约束
create table t_user1 (
id int primary key auto_increment,
name varchar(20)
);
insert into t_user1 values(NULL,'张三');
insert into t_user1(name) values('李四');
- 特点
默认情况下,auto_increment的初始值是 1,每新增一条记录,字段值自动加 1。(primary key) PK
一个表中只能有一个字段使用 auto_increment约束,且该字段必须有唯一索引,以避免序号重复(即为主键或主键的一部分)。
auto_increment约束的字段必须具备 NOT NULL 属性。
auto_increment约束的字段只能是整数类型(TINYINT、SMALLINT、INT、BIGINT 等。
auto_increment约束字段的最大值受该字段的数据类型约束,如果达到上限,auto_increment就会失效。
-- 指定自增长的初始值
-- 方式一:创建表时指定
create table t_user2 (
id int primary key auto_increment,
name varchar(20)
)auto_increment = 100;
insert into t_user2 values(NULL,'张三');
insert into t_user2 values(NULL,'张三');
-- 方式二:创建表之后指定
create table t_user3 (
id int primary key auto_increment,
name varchar(20)
);
alter table t_user3 auto_increment = 200;
insert into t_user3 values(NULL,'张三');
insert into t_user3 values(NULL,'张三');
- 指定自增字段初始值
如果第一条记录设置了该字段的初始值,那么新增加的记录就从这个初始值开始自增。例如,如果表中插入的第一条记录的 id 值设置为 5,那么再插入记录时,id 值就会从 5 开始往上增加
语法:
– 方式1,创建表时指定
create table t_user2 (
id int primary key auto_increment,
name varchar(20)
)auto_increment=100;
指定自增字段初始值
– 方式2,创建表之后指定
create table t_user3 (
id int primary key auto_increment,
name varchar(20)
);
alter table t_user2 auto_increment=100;
- delete和truncate在删除后自增列的变化
1.delete数据之后自动增长从断点开始
2.truncate数据之后自动增长从默认起始值开始
-- 指定自增长的初始值
-- 方式一:创建表时指定
create table t_user2 (
id int primary key auto_increment,
name varchar(20)
)auto_increment = 100;
insert into t_user2 values(NULL,'张三');
insert into t_user2 values(NULL,'张三');
-- 方式二:创建表之后指定
create table t_user3 (
id int primary key auto_increment,
name varchar(20)
);
alter table t_user3 auto_increment = 200;
insert into t_user3 values(NULL,'张三');
insert into t_user3 values(NULL,'张三');
-------------------------------------------
以下是delete和truncate的区别
delete from t_user2;
-- delete删除数据之后,自增长还是在最后一个值的基础上加1
-- 会从上一次那个位置继续 比如上面id是2
-- 执行完delete from t_user2;在执行下面两行代码时
-- id 分别为 3,4
insert into t_user2 values(NULL,'张三');
insert into t_user2 values(NULL,'张三');
truncate t_user2; -- 无论什么情况truncate删除之后,自增长从1开始,就算表之前设置起始值也没关系
insert into t_user2 values(NULL,'张三');
insert into t_user2 values(NULL,'张三');
🎡4、MySQL约束-非空约束(not null)
- 概念
MySQL 非空约束(not null)指字段的值不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。
语法
方式1:<字段名><数据类型> not null;
方式2:alter table 表名 modify 字段 类型 not null;
use mydb1;
-- 1. 创建非空约束-方式1,创建表时指定
create table mydb1.t_user6 (
id int ,
name varchar(20) not null, -- 指定非空约束
address varchar(20) not null -- 指定非空约束
);
insert into t_user6(id) values(1001); -- 不可以
insert into t_user6(id,name,address) values(1001,NULL,NULL); -- 不可以
insert into t_user6(id,name,address) values(1001,'NULL','NULL'); -- 可以(字符串:NULL)
insert into t_user6(id,name,address) values(1001,'',''); -- 可以(空串)
-- 2.创建非空约束-方式2,创建表之后指定
create table t_user7 (
id int ,
name varchar(20) , -- 指定非空约束
address varchar(20) -- 指定非空约束
);
alter table t_user7 modify name varchar(20) not null;
alter table t_user7 modify address varchar(20) not null;
-- 3.删除非空约束
-- alter table 表名 modify 字段 类型
alter table t_user7 modify name varchar(20) ;
alter table t_user7 modify address varchar(20) ;
🎡5、MySQL约束-唯一约束(unique)
- 概念
唯一约束(Unique Key)是指所有记录中字段的值不能重复出现。例如,为 id 字段加上唯一性约束后,每条记录的 id 值都是唯一的,不能出现重复的情况。
语法:
方式1:<字段名> <数据类型> unique
方式2: alter table 表名 add constraint 约束名 unique(列);
删除唯一约束
– alter table <表名> drop index <唯一约束名>;
-- 语法:
/*
方式1:<字段名> <数据类型> unique
方式2:alter table 表名 modify 字段 类型 not null;
*/
use mydb1;
-- 1. 添加唯一约束-方式1-创建表时指定
-- 这种方法默认列名就是约束名
create table t_user8 (
id int ,
name varchar(20) ,
phone_number varchar(20) unique -- 指定唯一约束
);
-- phone_number不可以相等
insert into t_user8 values(1001,'张三',138);
insert into t_user8 values(1002,'张三2',139);
-- 在MySQL中NULL和任何值都不相同 甚至和自己都不相同
-- 也可以理解为null本身就不是任何值不能拿出来比较
-- 所以下面两行代码同时执行没错
insert into t_user8 values(1003,'张三3',NULL);
insert into t_user8 values(1004,'张三4',NULL);
-- 2. 添加唯一约束-方式1-创建表之后指定
-- 格式:alter table 表名 add constraint 约束名 unique(列);
create table t_user9 (
id int ,
name varchar(20) ,
phone_number varchar(20) -- 指定唯一约束
);
---unique_pn只不过是这个约束的名字
alter table t_user9 add constraint unique_pn unique(phone_number);
insert into t_user9 values(1001,'张三',138);
-- 下面这个错了1001 的 phone_number已经为138了所以下面这条代码不能执行
insert into t_user9 values(1002,'张三2',138);
-- 3. 删除唯一约束
-- 格式:alter table <表名> drop index <唯一约束名>;
alter table t_user9 drop index unique_pn;
🎡6、MySQL约束-非空约束(not null)
- 概念
MySQL 默认值约束用来指定某列的默认值。
语法:
方式1: <字段名> <数据类型> default <默认值>;
方式2: alter table 表名 modify 列名 类型 default 默认值;
删除默认约束
– alter table <表名> modify column <字段名> <类型> default null;
-- 方式1-创建表时指定
create table t_user10 (
id int ,
name varchar(20) ,
address varchar(20) default '北京' -- 指定默认约束
);
insert into t_user10(id,name,address) values(1001,'张三',);-- 默认赋address值为‘北京’
insert into t_user10(id,name,address) values(1001,'张三','上海');-- address值为‘上海’
insert into t_user10 values(1002,'李四',NULL);-- address值为NULL
-- 方式2-创建表之后指定
-- alter table 表名 modify 列名 类型 default 默认值;
create table t_user11 (
id int ,
name varchar(20) ,
address varchar(20)
);
alter table t_user11 modify address varchar(20) default '深圳';
insert into t_user11(id,name) values(1001,'张三');-- 默认赋address值为‘深圳’
-- 2.删除默认约束
-- alter table <表名> change column <字段名> <类型> default null;
alter table t_user11 modify address varchar(20) default null;
insert into t_user11(id,name) values(1002,'李四');-- 默认赋address值为null
🎡7、MySQL约束- 零填充约束(zerofill)
- 概念
1、插入数据时,当该字段的值的长度小于定义的长度时,会在该值的前面补上相应的0
2、zerofill默认为int(10)
3、当使用zerofill 时,默认会自动加unsigned(无符号)属性,使用unsigned属性后,数值范围是原值的2倍,例如,有符号为-128~+127,无符号为0 ~ 256。
操作:
create table t_user12 (
id int zerofill , – 零填充约束
name varchar(20)
);
删除
alter table t_user12 modify id int;
-- 零填充约束(zerofill)
-- 1. 添加约束
create table t_user12 (
id int zerofill , -- 零填充约束
name varchar(20)
);
insert into t_user12 values(123, '张三');
insert into t_user12 values(1, '李四');
-- 2.删除约束
alter table t_user12 modify id int;
insert into t_user12 values(2, '王五');