mysql数据库同一用户可以存在多个库,用户可登录数据库,选择操作数据库和列表,查看有哪些数据库和相应的列表,对数据库和列表进行增删改查操作。mysql官网下载地址:https://dev.mysql.com/downloads/installer/
创建数据库
create database 数据库的名称; //数据库的名称是自己起的,sql语句以“;”结尾
show databases;
drop database 数据库的名称;
use 数据库的名称 //不需要以“;”结尾
创建数据库列表
创建数据库列表首先要选择使用哪个数据创建,然后才能创建
use 数据库的名称
create table 表的名称(字段名称 字段类型,字段名称 字段类型,...);//多个名称和类型时用“,”隔开
show tables;
drop table 表名;
select * from 表名;
查看表的结构
desc 表名;
对MySQL表字段进行增、删、改操作
增加字段
alter table 表名 add 字段名 类型;
alter table 表名 modify 修改字段名 类型;
向表中插入数据
insert into 表名(字段) values(字段对应的值);//多个字段用“,”隔开
delete from 表名 where 条件表达式;
update 表名 set 字段名= 字段值;
update 表名 set 字段名= 字段值,... where 条件表达式;
查询该表名下的所有数据
select * from 表名;// * 代表所有字段
查询该表下某个字段的所有数据
select 字段名 from 表名;
查询该表下多个字段的所有数据
select 字段名1,字段名2,... from 表名;
select 表达式 from 表名;
select distinct 字段名 from 表名;
select * from 表名 where 条件表达式;//大于、等于、小于、大于等于、小于等于
select * from 表名 where 条件表达式1 and 条件表达式2;
select * from 表名 where 条件表达式1 or 条件表达式2;
select * from 表名 where 字段名 in(条件值1,条件值2,...);//满足其中一个条件即可被查询到
select * from 表名 where 字段名 not in(条件值1,条件值2,...);//不满足其中任何一个条件即可被查询到
select * from 表名 where 字段名 最小值 and 最大值;//和 >= and <= 查询结果相同
但查询某个字段是否为空时,不能用“=”,而是要用关键字“is”作为判断
select * from 表名 where 字段名 is null;
select * from 表名 where 字段名 is not null;
select * from 表名 order by 字段名 desc;
select * from 表名 order by 字段名 asc;