Loading

SQL 基础语法

SQL 基础语法

在我们用 docker run --name ctf-mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:5.7 命令创建了 ctf-mysql 容器后,使用 start ctf-mysql 打开容器,用 docker exec -it ctf-mysql mysql -u root -p 进入容器。

常用的 docker 管理命令

# 查看正在运行的容器
docker ps

# 查看所有容器(包括停止的)
docker ps -a

# 停止容器
docker stop ctf-mysql

# 启动已停止的容器
docker start ctf-mysql

# 重启容器
docker restart ctf-mysql

# 查看容器日志(排错时很有用)
docker logs ctf-mysql

数据库语法

show databases;: 查看现在有哪些数据库,初始是有四个数据库的。
create databases ctf_practice;: 创建名为 ctf_practice 的数据库。
use ctf_practice;: 使用数据库。
select database();: 查看当前使用的数据库。
drop database if exists ctf_practice;: 删除名为 ctf_practice 的数据库。

会出现像这样的场景。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql> create database ctf_practice;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ctf_practice       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use ctf_practice;
Database changed
mysql> select database();
+--------------+
| database()   |
+--------------+
| ctf_practice |
+--------------+
1 row in set (0.00 sec)

mysql> create database temp;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ctf_practice       |
| mysql              |
| performance_schema |
| sys                |
| temp               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use temp;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| temp       |
+------------+
1 row in set (0.00 sec)

mysql> drop database if exists ctf_practice;
Query OK, 0 rows affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| temp               |
+--------------------+
5 rows in set (0.00 sec)

重要命令

select: 从数据库中提取数据。
update: 更新数据库中的数据。
delete: 从数据库中删除数据。
insert into: 向数据库中插入新数据。
create database: 创建新数据库。
alter database: 修改数据库。
create table: 创建新数据库表。
alter table: 变更数据库表。
drop table: 删除表。
create index: 创建索引。
drop index: 删除索引。

select: 从数据库中查询数据

select column_name(s)
from table_name
where condition
order by column_name [ASC|DESC]
  • column_name(s): 要查询的列。
  • table_name: 要查询的表。
  • condition: 查询条件。
  • order by: 排序方式

insert into: 用于向数据库表中插入新数据

insert into table_name (column1, column2, ...)
values (value1, value2, ...)
  • table_name: 要插入数据的表。
  • column1, column2: ……要插入数据的列。
  • value1, value2: ……对应列的值。

update: 用于更新数据库表中的现有信息

update table_name
set column1 = value1, column2 = value2, ...
where condtiton
  • table_name: 要更新数据的表。
  • column1 = value1, column2 = value2, ...: 要更新的列及其新值。
  • condition: 更新条件。

delete: 用于从数据库表中

delete from table_name
where condition
  • table_name: 要删除数据的表。
  • condition: 删除条件。

create table: 用于创建新的数据表

create table table_name (
    column1 data_type constraint,
    column2 data_type constraint,
    ...
)
  • table_name: 要创建的表名。
  • column1, column2: table_name 的列。
  • data_type: 列的数据类型。
  • constraint: 列的约束。

alter table: 用于修改现有数据库表的结构

alter table table_name
add column_name data_type
drop column column_name
  • table_name: 要修改的表。
  • column_name: 要添加/删除的列。
  • data_type: 列的数据类型。

create index: 用于创建索引,以加快查询速度

create index index_name
on table_name(column_name)
  • column_name: 要索引的列。

drop index: 用于删除索引

drop index index_name
on table_name
  • table_name: 索引所在的表。

其他

  • where: 用于指定筛选条件。
  • order by: 用于对结果集进行排序。
  • group by: 用于将结果集按一列或多列进行分组。
  • having: 用于对分组后的结果进行筛选。
  • join: 用于将两个或多个表的记录结合起来。
  • distinct: 用于返回唯一不同的值。
posted @ 2025-11-21 22:46  yi_fan0305  阅读(6)  评论(0)    收藏  举报