本篇博客说明:
!!!.注意此系列都用的是MySQL语句,和SQLServer,PostgreSQL有些细节上的差别!!!
1.每个操作都是先展示出语法格式
2.然后是具体例子
3.本篇注脚与文本顺讯息息相关或许会解答你的一些疑问
4.本文例子使用也是连续的,如果前面建立库没有删除就一直在,删除则之后也没有这个库
(!!!放心食用!!!)
MySQL语法
一·库的操作^1^
1.创建库^2^
CREATE^3^
语法格式:
create databases 数据库名字;
create database if not exists 数据库名字;
create database if not exists 数据库名字 character set utf8mb4 collate utf8mb4_0900_ai_ci;
mysql
create databases test01;

mysql
create database if not exists test01;
-- 这条语句与第一条语句很相似,但是这句更为保险
-- 两者区别在于这条语句会有警告不会报错,但是语句一再次创立同样的库时会报错,如下图

mysql
create database if not exists test02 character set utf8mb4 collate utf8mb4_0900_ai_ci;
-- 这句的意思为创建一个字符编码集为utf8mb4
-- 而utf8mb4_0900_ai_ci则为MySQL8.0默认的排序规则
2.修改库
ALTER
alter database 数据库名字 character set 字符集编码;
alter database 数据库名字 collate 排序规则;
mysql
alter database test01 character set gbk;

3.删除库
DROP
drop database 数据库名字;
drop database if exists 数据库名字;
mysql
drop database test01;

mysql
drop database if exists test02;

mysql
-- 根据重复删除来对比有无 if exists 的使用区别
drop database test01;
drop database if exists test01;

这里通过上图我们就发现如果加上"if exists"那么删除一个已经删除过的库就不会报错,而是会有一个警告,这个时候我们查看这个警告,我们就发现这个警告
二·表的操作
1.表的创建^4^
CREATE
create table 创建表的名字(表格包含的内容);
create table if not exists 创建表的名字(表格包含的内容) 引擎 字符集 排序规则;
这里的引擎,字符集,排序规则可以选择不写
mysql
-- 创建一个学生的表,包含学生的id,姓名,年龄,生日日期
create table student (
id bigint,
name varchar(20) comment '学生的姓名',
age int,
birthday date
);
-- 这里关于学生姓名我加了comment,其实相当于备注,可以不写

2.表的修改
ALTER
//增加一列
alter table 表的名字 add 要添加的列名 要添加的列的数据类型 after 在哪个列之后的列名;
//修改此列的长度
alter table 表的名字 modify 列名 修改后的长度;
//重命名列名
alter table 表名 rename column to;
//删除列
alter table 表名 drop 删除的列名;
//重命名表名
alter table 表名 rename to 新的表名;
mysql
alter table student add phone varchar(20) after borthday;

mysql
alter table student modify phone varchar(15);

mysql
alter table student rename column phone to phonenumber;

mysql
alter table student drop birthday;

mysql
alter table student rename to students;

3.表的删除
DROP
drop table 表名;
mysql
drop table students;

三·表中关于数据的操作
1.数据插入
INSERT
insert into 表名 (列1, 列2, 列3, ...)
values (值1, 值2, 值3, ...);
mysql
insert into student (id,name, age) values (1,'张三', 20);

2.数据查询
SELECT
select 列1, 列2, ...from 表名 where 条件;
select* from 表名; -- 查询全部表的内容
mysql
select*from student;
select name, age from student;

3.数据修改
UPDATA
update 表名 set 列1 = 值1, 列2 = 值2, ... where 条件;
mysql
update student set age = 21 where id = 1;

4. 数据删除
DELETE
delete from 表名 where 条件;
mysql
delete from student where id = 1;

今天内容就到这里啦,对表中数据的操作还有很多,之后咱们再说,加油!