当前位置:网站首页>MySQL(二)——基本操作
MySQL(二)——基本操作
2022-06-28 06:04:00 【木棣%】
MySQL操作
数据库操作
- 创建数据库
CREATE DATABASE
# impact为数据库名
# if not exists可选,防止mysql报错
CREATE DATABASE (if not exists) impact;
- 列出 MySQL数据库列表
SHOW DATABASES
show databases;
- 使用数据库
USE 数据库名
use impact;
Database changed
- 判断当前所处的数据库
SELECT DATABASE()
SELECT DATABASE();
- 删除数据库
DROP DATABASE 数据库名
# runoob为数据库名,IF EXISTS可选,防止mysql报错
DROP DATABASE (IF EXISTS) runoob;
数据库表操作
- 数据库中的所有表
SHOW TABLES
show tables;
- 创建数据表
CREATE TABLE 表名()
# 第一列为表字段名,第二列为数据类型
# 表字段用','隔开
# comment '说明' 可选,字段的属性 可选
create table charact_tbl(
id int comment '编号',
name varchar(10) comment '姓名',
country varchar(10) comment '国家',
sex varchar(1) comment '性别'
) comment '角色表';
- 查询表结构
desc 表名
# 可查询多个表,用逗号隔开
desc charact_tb;
- 查询指定表的建表语句
SHOW CREATE TABLE 表名
show create table charact_tb;
- 修改表名
ALTER TABLE 表名 RENAME TO 新表名
ALTER TABLE charact_tb RENAME TO alter_tbl;
- 删除表单
DROP TABLE (IF EXISTS) 表名
DROP TABLE IF EXISTS charact_tb;
- 删除该表并重新再创建(格式化)
TRUNCATE TABLE 表名
TRUNCATE TABLE charact_tbl;
- 添加表字段
ALTER TABLE 表名 ADD 字段名 类型会添加到表单的末尾
alter table charact_tb add age int;
- 修改字段类型及名称
ALTER TABLE 表名 MODIFY 字段名 类型 ;只修改数据类型
alter table charact_tb modify age tinyint;
ALTER TABLE 表名 MODIFY 字段名 新字段名 类型
alter table charact_tb change age school varchar(10);
- 删除表字段
ALTER TABLE 表名 DROP 表字段名 ;如果数据表中只剩一个字段则无法使用DROP来删除字段
alter table charact_tb drop school;
- 从表中查询数据
SELECT 列名称 FROM 表名称
# 查询多个列可以用逗号隔开
select name from alter_tbl;
SELECT * FROM 表名称'*'代表表中的所有列
select * from alter_tbl;
- 在表单中插入数据
INSERT INTO 表名 ( 表字段 )VALUES( 插入的数据 );
insert into user(name,age,school,grade,gender,date)
-> VALUES
-> ("小何",17,"xx大学","大一","女",now());
where子句
用于规定选择的标准,可用于增删查改
- 查找数据
SELECT 表字段 FROM 表名称 WHERE 表字段 运算符 值
select * from user where age>17;
AND和OR 运算符
信息的进一步过滤
- AND运算符
# 使用AND显示年龄大于17并且年级是大一
select * from user where age>17 AND grade="大一";
- OR运算符
# 使用OR显示年龄大于17或者年级是大一
select * from user where age>17 OR grade="大一";
- 同时使用OR和AND运算符
# 使用OR显示年龄大于17或者年级是大一并且年龄小于19
select * from user where(age>17 OR grade="大一") AND age<19;
LIKE 子句
模糊匹配
- %通配符
- 在字符后代表着匹配从这几个字符开始
# 查询id以1开始的
select * from user where id LIKE "1%" ;
- 在字符代前代表着匹配以这几个字符结束
# 查询id以1结尾的
select * from user where id LIKE "%1" ;
- 字符两边都有%表示包含
# 查询日期中含有16的
select * from user where date LIKE "%16%" ;
- _运算符
- 相当于代替任意1个字符
# _代替了年龄的第二位
select * from user where age LIKE "1_" ;
IN操作符
在 WHERE 子句中规定多个值
SELECT 表字段 FROM 表名 WHERE 表字段 IN (表字段值)
SELECT * FROM user WHERE age IN (16,17);
BETWEEN 操作符
选取介于两个值之间的数据范围,和AND连用
SELECT 表字段 FROM 表名 WHERE 表字段 BETWEEN 表字段值1 AND 表字段值2
包括表字段值1,包括表字段值2
select * from user where id BETWEEN 1 AND 3;
在BETWEEN前加NOT,选取范围之外
select * from user where id NOT BETWEEN 4 AND 11;
UNION 操作符
合并两个或多个 SELECT 语句的结果
重复值不会显示
UNION ALL可以显示重复值
排序
ORDER BY 用于定义排序的方式
# 默认升序
select * from employees order by last;
desc降序
select * from employees order by last desc;
asc升序
select * from employees order by last asc;
分组
group by 把数据分组
# 计数count()
select age ,count(*) from user group by age;
更新数据
UPDATE 表名 SET 字段名=新值 WHERE 字段名 操作符 值
update user SET school="xxx大学" where age=16;
删除数据
DELETE FROM 表名 WHERE 字段名 操作符 值
delete from user where age<17;
JOIN子句
- INNER JOIN
获取两个表中字段匹配关系的记录
两表之间必须匹配
select * from user inner join employees on employees.id=user.id;
以上等价于
select * from user ,employees where employees.id =user.id;
- LEFT JOIN
获取左表所有记录,即使右表没有对应匹配的记录
select * from employees left join user on employees.id=user.id order by employees.id ;
- RIGHT JOIN
获取右表所有记录,即使左表没有对应匹配的记录
select * from user right join employees on employees.id=user.id order by employees.id ;
SELECT DISTINCT 语句
列出不同的值
边栏推荐
- YYGH-BUG-03
- Xcode13.3.1 error reported after pod install
- Data midrange: implementation and summary of AI midrange
- Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
- Sharing tips for efficient scripting
- 简单手写debounce函数
- 自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
- Capacity scheduling absolute value configuration queue usage and pit avoidance
- Jenkins continues integration 2
- numpy. reshape, numpy. Understanding of transfer
猜你喜欢

Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics

lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性

The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded

Jenkins继续集成2

Windows环境Redis使用AOF持久化,无法生成AOF文件,生成后无法加载AOF文件内容

Sharing tips for efficient scripting

The custom cube UI pop-up dialog supports multiple and multiple types of input boxes

6. 毕业设计温湿度监控系统(ESP8266 + DHT11 +OLED 实时上传温湿度数据给公网服务器并在OLED显示屏上显示实时温湿度)

函数栈帧的创建和销毁

容量调度绝对值配置队列使用与避坑
随机推荐
pkg打包node工程(express)
Idea automatically adds comments when creating classes
What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
Openharmony gnawing paper growth plan -- json-rpc
Alibaba cloud SMS service (Complete Guide), SMS sending function implementation.
Small ball playing
Use the SQL SELECT count distinct query statement to count the total number of unique values of a field in the database
YYGH-8-预约挂号
easyui下拉框选中触发事件
Object对象转 List集合
Data middle office: an article that takes you to understand data middle office in simple terms
YYGH-BUG-03
Qtcanpool knowledge 07:ribbon
Windows环境Redis使用AOF持久化,无法生成AOF文件,生成后无法加载AOF文件内容
安装 Ffmpefg
Xcode13.3.1 项目执行pod install后报错
Independent station sellers are using the five e-mail marketing skills, do you know?
mac下安装多个版本php并且进行管理
Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
Filecoin hacker song developer competition