MySQL数据库操作基础

连接数据库
mysql -h 127.0.0.1 -u root -p

退出数据库
exit;

列出数据库
show databases;

列出表
show tables;

MySQL数据库操作基础
新建数据库
CREATE DATABASE 数据库名;
MySQL数据库操作基础
使用数据库
use 数据库名;
MySQL数据库操作基础
创建表
create table offices(officeCode varchar(10),city varchar(10),phone int(12),addressLine1 varchar(20));
create table 表名(字段名1 字段类型(大小),字段名2 字段类型(大小) …);
MySQL数据库操作基础
增加数据
insert into offices(officeCode,city,phone,addressLine1,addressLine2,state,country,postalCode,territory) values(8,“city1”,"+123456798",“addressLine12”);
#如果数据是字符型,必须使用单引号或者双引号
MySQL数据库操作基础
删除数据
delete from 表名 where 字段名=2;
#删除id为2的数据
MySQL数据库操作基础
修改数据
update test1 set name=“xiaobai” where id=1;
#修改id为1的name数据为xiaobai

MySQL数据库操作基础
查询数据
select * from test;
#查询表中所有字段

select * from test1 limit 0,1;
#从第0条数据开始查询1条数据

select name from test1;
#查询字段为name的数据
MySQL数据库操作基础
MySQL数据库操作基础

Mysql高级操作

排序
select * from offices order by phone desc;
#降序排序
MySQL数据库操作基础
limit语法在查询中的应用
select * from offices limit 3,5;
MySQL数据库操作基础
group by语法在查询中的应用
select * from offices group by city;
#分组查询
MySQL数据库操作基础
如果城市相同的话只显示一个城市

或者结合其他聚合函数查询
select officecode,sum(phone) from offices group by city;
#分组查询

union slect语法在查询中的应用
select * from offices where officecode=7 and 1=1 union select 1,2,3,4;
#联合查询
MySQL数据库操作基础
exists语法在查询中的应用
select * from offices where officecode=7 and exists(select * from XXX);
#联合查询