你能为我提供一套mysql的增删改查基础练习题吗,附带练习题需要的数据表的建表语句
时间: 2023-11-21 18:04:43 浏览: 101
mysql查询练习题
好的,以下是一些mysql的增删改查基础练习题以及对应的数据表的建表语句:
1. 创建一个学生表,包含学生id、姓名、年龄、性别、所在班级等字段,写出查询语句,查询年龄大于18岁的男生。
```mysql
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
class VARCHAR(50) NOT NULL
);
INSERT INTO students (name, age, gender, class) VALUES
('Tom', 20, 'male', 'ClassA'),
('Jane', 18, 'female', 'ClassA'),
('Jack', 19, 'male', 'ClassB'),
('Mary', 21, 'female', 'ClassB'),
('Bob', 17, 'male', 'ClassA');
SELECT * FROM students WHERE age > 18 AND gender = 'male';
```
2. 创建一个商品表,包含商品id、商品名称、价格、库存等字段,写出查询语句,查询价格在100元及以上且库存不足10件的商品。
```mysql
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL
);
INSERT INTO products (name, price, stock) VALUES
('ProductA', 120.50, 15),
('ProductB', 99.99, 5),
('ProductC', 150.00, 8),
('ProductD', 88.88, 12),
('ProductE', 200.00, 3);
SELECT * FROM products WHERE price >= 100 AND stock < 10;
```
3. 创建一个订单表,包含订单id、商品id、购买数量、购买时间等字段,写出查询语句,查询购买数量超过5件的商品。
```mysql
CREATE TABLE orders (
id INT PRIMARY KEY AUTO_INCREMENT,
product_id INT NOT NULL,
quantity INT NOT NULL,
purchase_time DATETIME NOT NULL
);
INSERT INTO orders (product_id, quantity, purchase_time) VALUES
(1, 3, '2022-01-01 10:00:00'),
(2, 6, '2022-01-02 14:30:00'),
(3, 2, '2022-01-03 08:15:00'),
(4, 1, '2022-01-04 12:00:00'),
(5, 7, '2022-01-05 16:45:00');
SELECT * FROM orders WHERE quantity > 5;
```
4. 创建一个员工表,包含员工id、员工姓名、所在部门、入职时间等字段,写出查询语句,查询入职时间在2021年之后的员工。
```mysql
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
hire_date DATE NOT NULL
);
INSERT INTO employees (name, department, hire_date) VALUES
('Tom', 'HR', '2020-12-01'),
('Jane', 'Finance', '2021-01-01'),
('Jack', 'IT', '2021-02-15'),
('Mary', 'Marketing', '2021-06-30'),
('Bob', 'Sales', '2022-01-01');
SELECT * FROM employees WHERE hire_date > '2021-01-01';
```
阅读全文