create database 创建数据库
create database fm; -- 创建数据库 数据库的名字 为 fm
创建成功之后,使用数据库
use fm; -- 使用 fm 数据库
create table 创建表
创建表的基本语法
create table 表名( -- 定义字段名id int not null auto_increment, -- id 为int 类型, 并 不为null, 自动递增;age int default null, -- age 为int 类型, 默认为空name varchar(255) default null, -- name为 varchar 类型, 默认为 空salary decimal(10,2) default null, -- salary 为 decimal 的类型, 10,2 表示长度10 ,小数点2位,primary key(id));
常用数据类型
| 类型 | 说明 |
|---|---|
| varchar | 字符串 |
| int | 数字 |
| decimal | 带有小数点的数字 |
| datetime | 日期时间 |
创建表
表名 姓名拼音_orders
| 字段 | 类型 | 是否为空 | 是否主键 | 是否递增 | 说明 |
|---|---|---|---|---|---|
| id | int | no | yes | yes | 字段序号 |
| o_id | varchar(255) | no | no | no | 订单号 |
| price | decimal(10,2) | no | no | no | 价格金额 |
| create_time | datetime | no | no | no | 订单日期 |
create table zhaozengyang_orders(id int not null auto_increment,o_id varchar(255) not null,price decimal(10,2) not null,create_time datetime not null,primary key(id));
insert into 插入数据
插入数据的基本语法
INSERT INTO 表名(o_id, price, create_time) -- 字段名VALUES('20220310163401', 20.09, '2022-03-10 16:34:43'); -- 值

insert into a(o_id,price,create_time)VALUES("202211122233334",99.99,now()); -- now() 表示当前时间SELECT * from a; -- 查询表中的数据
update修改数据
基本语法
UPDATE 表名SET price = 100 -- 更新后的值WHERE id = 4 -- 条件
-- 将价格低于 100 的价格 更改位 1000update aset price=1000where price <= 100;
不加条件 更改的是全部的内容
update aset price=10000
delete 删除数据
DELETE FROM 表名
WHERE `id` = 8 -- 条件
删除salary 大于100的数据
delete from a
where salary > 100;
alter table修改table中的列名
修改列名
ALTER TABLE 表名
CHANGE COLUMN 原来的列名 新的列名 decimal(10, 0) NOT NULL AFTER `age`;
修改o_id 字段,改为 username 类型为 varchar(50) 不为null
alter table a
change column o_id username varchar(50) not null;

drop table 删除表
删除表a
drop table a;

drop database 删除数据库
drop database fm; -- 删除数据库 fm
思维导图

作业

- 自己创建表。
- 完成上面的操作。 ```python
SELECT whq_student.name,max(score) from whq_score
INNER JOIN whq_student
on whq_score.stu_id=whq_student.id
WHERE
score>90
GROUP BY whq_student.name
ORDER BY max(score) desc limit 10;
```python
SELECT count(stu_id) as 两门90以上的人数 from
(SELECT stu_id from whq_score
WHERE score>90 GROUP BY stu_id having count(stu_id)>=2)as tmp1
update whq_score
set score=100
WHERE
stu_id in (SELECT id from whq_student where name='张三')
AND
course='英语'
