文章目录
1、增删改查操作
1.1、查询
连接数据库
mysql -u[用户名] -p[密码]
查询数据库列表
show databases;
查询当前数据库中的所有表
show tables;
查看表结构
desc 表名;
查询数据表中的所有信息
select * from 表名;
1.2、增加
创建数据库
create database 库名;
创建表
create tables 表名(column_name column_type);
注释:
PRIMARY KEY - 主键约束。
NOT NULL - 每一行都必须含有值(不能为空)。
UNSIGNED - 使用无符号数值类型,0 及正数。
AUTO INCREMENT - 设置 MySQL 字段的值在新增记录时每次自动增长 1
插入数据
insert into 表名(field1,field2) values (value1,value2);
1.3、修改
修改数据表中的内容
update [表名] set [字段] = [值];
1.4、删除
删除数据库
drop database 数据库名;
删除表
drop table 表名;
删除数据表中的数据
delete from [表名] where [条件];
2、where 子句的使用
查询数据表中的指定内容
select * from [表名] where [条件]
like 模糊查询
查询staff_info表中staff_name字段内容等于李四的信息
select id,staff_name,age,phone from staff_info where staff_name like ‘李四’;
使用’%’通配符查询出staff_info表中staff_name字段内容等于’李’字开头的所有信息
select id,staff_name,age,phone from staff_info where staff_name like ‘李%’;
使用’_’通配符查询出staff_info表中staff_name字段内容等于’王’字开头的所有信息
select id,staff_name,age,phone from staff_info where staff_name like ‘王_’;
注:%表示任意多个任意字符,_表示一个任意字符
范围查找 in 关键字的使用
使用 in 关键字查询staff_info表中 age 字段中等于20和24的内容
select id,staff_name,age from staff_info where age in (20,24);
范围查找 between 关键字的使用
使用 between 关键字查询出staff_info表中age字段包含大于等于20小于等于22的内容
select id,staff_name,age from staff_info where age between 20 and 22;
order by 关键字使用
order by一般是对指定的列进行排序;desc是降序,asc是升序
select id,staff_name,age from staff_info where age>20 order by age desc;