首先启动数据库

  • Linux :
1
sudo systemctl start mysql
1
mysql -h 数据库地址 -u 用户名 -p 密码
  • windows:
    • 进入mysql 目录下的 bin 目录,打开cmd

查看数据库

1
show databases;

选择数据库

1
use [数据库名];

查看数据表

1
show tables;

查看表结构

1
desc [数据表名];

创建一个数据库

1
create database [数据库名];

创建一个数据表

1
2
3
4
create table [数据表名] (
字段名1 字段类型,
字段名2 字段类型
);

eg:

1
2
3
4
5
create table user (
id int(20),
name varchar(255),
password varchar(255)
);

增加数据

1
insert aaa values (值1,值2...);		

注意 插入的字段数必须和表结构中原有的字段数相同

方法2:指定字段添加数据

1
insert aaa (字段1,字段2) values (值1,值2);

注意:值如果是整型不用加引号,如果是字符串必须加引号

删除数据库

1
drop database [数据库名];	

删除数据表

1
drop table [数据表名];	

删除数据

1
delete from [表名] where 条件

更新数据

1
2
3
4
update 表名
set 字段名1 = 字段值1 , [字段名2 = 字段值2];
where 条件语句;
# where + 条件 更新部分数据 || 不加where子句更新全部数据

普通查询

1
select [字段] from [表] where 条件

子查询

1
select [字段] from [表] where [字段] = (select [字段] from [表] where [条件])

eg:

1
select * from user where id=(select id from artical1 where value='helloworld');

联合查询

条件:表1 和 表2 的字段数必须相同

1
select 1,2,3 union select 1,2,3

模糊查询

下划线通配符 (_)

1
select * from test where str like '%str%';