一、数据库的操作

数据库中的注释方式:

1.使用#

2.使用–

1.数据库的创建:

# 链接mysql数据库后,进入mysql后可以操作数据 

# 1. 创建库 
create database if not exists tlxy default charset=utf8;

-- 1. 数据库 tlxy 如果不存在则创建数据库,存在则不创建 
-- 2. 创建 tlxy 数据库,并设置字符集为utf8 
-- 3. 无特殊情况都要求字符集为utf8或者utf8mb4的字符编码

2.查看所有库

# 1. 查看所有库
show databases;

3.打开库/进入库/选择库

# use 库名
use tlxy

4.删除库

# 删除库,那么库中的所有数据都将在磁盘中删除
drop database 库名

二、数据表的操作

1.创建表

语法格式:

create table 表名(

字段名1 类型 字段约束,

字段名 2类型 字段约束,

… //最后一个字段没有逗号~

)engine=innodb default charset=utf8mb4;

创建表的基本原则:

  • 表明和字段名 尽可能的符合命名规范,并且最好能够‘见名之意’ 。(一般结合下划线使用~)
  • 表中数据必须有唯一标示,即主键定义。无特殊情况,主键都为数字并自增即可 。
  • 表中字段所对应的类型设置合理,并限制合理长度
  • 表引擎推荐使用innodb,并无特殊情况都要求为utf8或者utf8mb4的字符编码
# 以下创建一个 users 的表
create table users(
-- 创建ID字段,为正整数,不允许为空 主键,自动递增
id int unsigned not null primary key auto_increment,
-- 创建 存储 名字的字段,为字符串类型,最大长度 5个字符,不允许为空
username varchar(5) not null,
-- 创建存储 密码 的字段,固定长度 32位字符, 不允许为空
password char(32) not null,
-- 创建 年龄 字段,不允许为空,默认值为 20
age tinyint not null default 20
)engine=innodb default charset=utf8;

2.查看表结构

# 查看表结构
desc users;

3.查看建表语句

#查看建表语句
show create table users;

4.查看表的状态

# 直接查看当前表状态信息
show table status from haha where name = 'user2'\G;

5.修改表结构

语法格式:

alter table 表名 action (更改的选项)

更改选项_包括:

  • 添加字段(add)、删除字段(drop)、修改字段(modify\change)
  • 修改表名(rename as)
  • 修改自增值(auto_increment = 修改值)
  • 修改表引擎(engine = ’ 引擎名’)
添加字段
# 语法:alter table 表名 add 添加的字段信息(字段名 类型~必不可少)

-- 在 users 表中 追加 一个 num 字段 (末尾添加~)
alter table users add num int not null;

-- 在指定字段后面追加字段 在 users 表中 age字段后面 添加一个 email 字段
alter table users add email varchar(50) after age;

-- 在指定字段后面追加字段,在 users 表中 age字段后面 添加一个 phone (指定字段后~)
alter table users add phone char(11) not null after age;

-- 在表的最前面添加一个字段(最开始添加~)
alter table users add aa int first;
删除字段
# 删除字段 alter table 表名 drop 被删除的字段名
alter table users drop aa;
修改字段
语法格式: alter table 表名 change|modify 被修改的字段信息
change: 可以修改字段名,
modify: 不能修改字段名。

# 修改表中的 num 字段 类型,使用 modify 不修改表名
alter table users modify num tinyint not null default 12; 

# 修改表中的 num 字段 为 int并且字段名为 nn
alter table users change num mm int;

# 注意:一般情况下,无特殊要求,不要轻易修改表结构
修改表名
# 语法:alter table 原表名 rename as 新表名
alter table user1 rename as user3;
更改表中的自增的值
# 在常规情况下,auto_increment 默认从1开始继续递增
alter table users auto_increment = 1000;
修改表引擎
# 修改表引擎语句 
alter table users engine = 'myisam';
alter table users engine = 'innodb';
#方式1查看:
# 通过查看建表语句获取当前的表引擎
show create table user2\G;
#方式2查看:
# 直接查看当前表状态信息
show table status from haha where name = 'user2'\G;

6.删除表

#drop table 表名
drop table user1;

三、 数据的操作

1.插入

格式: insert into 表名[(字段列表)] values(值列表…);

标准_添加
#insert into 表名(字段1,字段2,字段3)  values(值1,值2,值3); 
#指定所有字段,给定所有的值(中间要加空格~)
insert into stu(id,name,age,sex,classid) values(1,'zhangsan',20,'m','lamp138');
指定部分字段值_添加
#中间要加空格~
#insert into 表名(字段1,字段2) values(a值1,a值2); 
insert into stu(name,classid) value('lisi','lamp138');
不指定字段值_添加
#insert into 表名 values(值1,值2,值3); 
#此时要按字段顺序写出所以值~
insert into stu value(null,'wangwu',21,'w','lamp138');
批量_添加值
#insert into 表名(字段1,字段2,字段3) values(a值1,a值2,a值3),(b值1,b值2,b值3); 
insert into stu values
-> (null,'zhaoliu',25,'w','lamp94'),
-> (null,'uu01',26,'m','lamp94'),
-> (null,'uu02',28,'w','lamp92'); #最后一个添加值加分号~

2.查询

检索_单个列
#select 字段 from 表名;
select id from user2;
检索_多个列
#select 字段1,字段2,字段3 from 表名; 
select id,username from user2;
检索_所有列
#select * from 表名; 
select * from user2;

友情提示:一般不建议使用*通配符进行查询。

检索_指定条件查询
#select * from 表名 where 字段=某个值;  
select * from user2 where id=1;
检索_不同的行(去重)
#select distinct 字段名 from 表名;
select distinct class_id from user2;
检索_指定的行数
#select * from 表名 limit 行数;
#检索前3行
select * from user2 limit 3;
#跳过前3行,检索后5行
select * from user2 limit 3,5;
检索_使用完整的表名
#select 表名.字段名 from 表名;
#select 表名.字段名 from 库名.表名;

3.修改

格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n… where 条件

修改_某个指定的字段
#update 表名 set 字段=某个值 where 条件;  
update user2 set id=10 where id=1;
修改_一行数据的多个指定字段
#update 表名 set 字段1=值1,字段2=值2 where 条件;
update user2 set id=2,username='李四' where id=1;
修改_多行字段值
#将id值为12和14的数据值sex改为m,classid改为lamp92
update stu set sex='m',classid='lamp92' where id=12 or id=14
update stu set sex='m',classid='lamp92' where id in(12,14); #两者等价
修改_字段加上某个值
#update 表名 set 字段=字段+值 where 条件;

4.删除

删除_数据表中指定的一行数据
#delete from 表名 where 字段=某个值;
 delete from user2 where id=1;
删除_多行数据
#删除stu表中id值为20到30的数据
mysql> delete from stu where id>=20 and id<=30;
delete from stu where id between 20 and 30;#两者等价