MySQL 表的增删改查 / 数据库约束

本文详细介绍了MySQL数据库的管理,包括创建、查看和删除数据库,以及数据表的操作。重点讲解了数据的增删改查,如数据插入、查找、修改和删除,并涉及到各种查询技巧,如条件查询、排序、分页等。此外,还涵盖了数据库约束,如NOT NULL、UNIQUE、DEFAULT、PRIMARY KEY和FOREIGN KEY,强调了它们在确保数据完整性和参照完整性方面的作用。
摘要由CSDN通过智能技术生成


  数据库,是一类组织管理存储数据的软件,数据存储在磁盘上。而MySQL是一个 C / S 结构的软件,关系型数据库,是以 数据表 的形式来进行组织数据的。

1 针对数据库进行操作

(1) 创建数据库 create database [数据库名];
(2) 查看数据库 show databases;
(3) 删除数据库 drop database [数据库名];
a. MySQL 支持 bin log
b. 备份,mysqldump 工具
c. 基于磁盘文件进行还原,删除操作后,数据被标记为无效,如果用工具进行抢救可以找回,但若是创建了新的数据将其覆盖,便无法找回了。
(4) 选中数据库 use [数据库名];

2 针对数据表进行操作

(1) 查看都有什么表 show tables;
(2) 创建表 create table [表名] (列);
常用数据类型:int 、decimal、varchar、datetime;
(3) 查看表结构 desc [表名];
(4) 删除表 drop table [表名];

3 数据的增删改查

3.1 数据的插入

insert into [表名] (列名) values (值);

3.2 数据的查找 (重要)

(1). 全列查找 select * from [表名];

(2). 指定列查找 select [列名] from [表名];

(3). 带有表达式的查找 select id,name,english + 10 from [表名];

(4). 查找时给指定列起别名 select [列名] as [别名] from [表名];

(5). 查找某一列并且去重 select distinct [列名] from [表名];

(6). 排序:利用 order by 指定某一列进行排序,默认按照升序排序 select name,math from exam_result order by math;加上 desc 就是降序排序 select name,math from exam_result order by math desc;

(7).条件查询
 a.基本查询
select name,english from exam_result where english < 60;
select name,chinese + math + english from exam_result where chinese + math + english < 200;
 b.and 和 or
select name,chinese,english from exam_result where chinese > 80 and english > 80;
select name,chinese,english from exam_result where chinese > 80 or english > 80;
(如果 and 和 or 同时存在,注意and 优先级比 or 高)
 c.范围查询 —— between…and…
select name,chinese from exam_result where chinese between 80 and 90;
 d.范围查询 —— in
select name,chinese from exam_result where math in (70,89,90);
 e.模糊查找(只要有一部分相同即可,效率较低)
select name from exam_result where name like ‘j%’; (找出以 j 开头的字符串)
select name from exam_result where name like ‘%j’; (找出以 j 结尾的字符串)
select name from exam_result where name like ‘%j%’; (找出包含 j 的字符串)
select name from exam_result where name like ‘j_’; (找出 j 开头的并且后面跟着一个字符的符串,每个下划线代表一个字符)
select name from exam_result where chinese like ‘8%’; (找出 chinese 值为 80 多的数据)
 f.空值查询
select * from exam_result where chinese = math;(不安全的等号,所以结果大概率有误)
   select * from exam_result where chinese <=> math;(安全的等号,选择出 chinese值 和 math值 相等的)
select * from exam_result where chinese is null;

(8). 分页查询
select * from exam_result limit 3; (查询前三行)
select * from exam_result limit 3 offset 0;(查询 0 开始的三条数据)
select * from exam_result limit 2,3;(从第 2 条开始查找三条数据)

3.3 数据的修改

update exam_result set chinese = 99 where name = ‘tom’;(将 tom 的 chinese值 改为99)
update exam_result set math = math + 10 order by chinese + math + english limit 3;(将总成绩后三名的同学的数学成绩加 10 分)

3.4 数据的删除

delete from exam_result where name = ‘tom’ and id = 10;

4 数据库约束

4.1 约束类型

not null :指某列不能存储空值;
unique:保证某列的每行必须有唯一的值;
default:规定没有给列赋值时的默认值;
primary key:not null 和 unique 的结合,确保某列(或两个列多个列的结合有唯一标识,有助于更容易、更快速地找到表中的一个特定的记录);
foreign key:保证一个表中的数据匹配另一个表中的值的参照完整性;

4.2 null 约束

在创建表的时候指定某列不为空:
create table student (id int not null,name varchar(50));

4.3 unique 唯一约束

指定某列,要求其每一行都是唯一的、不重复的:
create table student (id int not null,name varchar(50) unique);

4.4 default 默认值约束

例如,指定插入数据时,name 列为空,默认值为 unkown:
create table student (id int not null,name varchar(50) unique default ‘unkown’);

4.5 primary key 主键约束

设置 id 为主键,主键是 not null 和 unique 的结合,是一条记录的唯一身份标识,一个表中只能有一个主键:
create table student (id int primary key,name varchar(50) unique);

对于整数类型的 主键,搭配 auto_increment 来使用,插入数据对应字段不给定值时,使用最大值 + 1。
create table student (id int primary key auto_increment,name varchar(50) unique);

4.6 foreign key 外键约束

把上面的学生表删除,我们再来创建一个班级表 class ,id 为主键 :
create table class (id int primary key ,name varchar(50));
create table student (id int primary key auto_increment,name varchar(50),classId int,foreign key (classId) references class(id));

外键用于关联其他表的主键或唯一键:
foreign key (classId) references class(id) ,其中对当前表的classId 这一列进行约束,class(id) 和另外一个 class 表建立其中的 id 列关联起来。(外键约束就是要求当前表里面的 classId 字段的值,必须在 class 表的 id 中出现过才可以,并且注意:id 必须是 class 表的主键

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值