Below I will share with you a simple exercise, which is very helpful for beginners, including how to create a table, add a primary key, insert data, and implement addition, deletion, modification, and query operations.
1. Create a table
- Create a product table according to the requirements, the requirements are as follows.
Column definition Column name Data type Constraints |
Commodity id product_id fixed-length string type ( 4 ) non-empty, primary key |
Product name product_name variable length string type ( 100 ) non-empty |
Product type product_type variable length string ( 4 ) non-empty |
Sales price sale_price Integer type |
purchase price purchase_price integer type |
Registration date regist_date date type |
Table:product |
CREATE TABLE Product
(produnct_id CHAR(4) NOT NULL PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
product_type VARCHAR(32) NOT NULL,
sale_price INTEGER ,
purchase_price INTEGER ,
regist_date DATE);
2. Insert data
- Insert the following data into the product table
'0001', 'T-shirt' ,'clothes', 1000, 500, '2009-09-20'
'0002', 'Hole Punch', 'Office Supplies', 500, 320, '2009-09-11'
'0003', 'Sports T-shirt', 'Clothes', 4000, 2800, NULL
'0004', 'Chopper', 'Kitchen Utensils', 3000, 2800, '2009-09-20'
'0005', 'Pressure Cooker', 'Kitchen Appliance', 6800, 5000, '2009-01-15'
'0006', 'fork', 'kitchenware', 500, NULL, '2009-09-20'
'0007', 'grater', 'kitchen utensils', 880, 790, '2008-04-28'
'0008', 'Ballpoint Pen', 'Office Supplies', 100, NULL, '2009-11-11'
INSERT INTO Product VALUES('0001', 'T恤' ,'衣服', 1000, 500, '2009-09-20');
INSERT INTO Product VALUES('0002', '打孔器', '办公用品', 500, 320, '2009-09-11');
INSERT INTO Product VALUES('0003', '运动T恤', '衣服', 4000, 2800, NULL);
INSERT INTO Product VALUES('0004', '菜刀', '厨房用具', 3000, 2800, '2009-09-20');
INSERT INTO Product VALUES('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');
INSERT INTO product VALUES('0006', '叉子', '厨房用具', 500, NULL, '2009-09-20');
INSERT INTO product VALUES('0007', '擦菜板', '厨房用具', 880, 790, '2008-04-28');
INSERT INTO Product VALUES('0008', '圆珠笔', '办公用品', 100, NULL, '2009-11-11');
3. Update data
- Change product_name hole punch to stapler
UPDATE <表名> SET <列名=更新值> WHERE <更新条件>
UPDATE product SET product_name='订书机' WHERE product_name='打孔器';
2. Increase the price of sale_price less than or equal to 500 yuan by 50
UPDATE product SET sale_price=sale_price+50 WHERE sale_price<=500;
Fourth, view the data
1. Write a SQL statement to select the products whose registration date (regist_date) is after April 28, 2009 from the Product table. The query result should contain two columns product_name and regist_date.
SELECT <列名>
FROM <表名>
WHERE <查询条件表达式>
ORDER BY <排序的列名> ASC或者DESC
SELECT product_name,regist_date FROM product WHERE regist_date>'2009-4-28';
2. Please write a SELECT statement to select records from the Product table that meet the condition of "office supplies and kitchen utensils whose profit is higher than 100 yuan after a 10% discount on the sales unit price". The query result should include the product_name column, product_type column, and the profit after the 10% discount on the sales unit price (the alias is set to profit)
SELECT product_name,product_type,sale_price*0.9-purchase_price profit FROM product WHERE (product_type='办公用品' OR product_type='厨房用具')
AND sale_price*0.9-purchase_price>100;
3. Filter out the product_name, sale_price, purchase_price of products whose sale_price is 500 yuan or more higher than purchase_price.
SELECT product_name,sale_price,purchase_price FROM product WHERE sale_price-purchase_price>=500;
4. Print out the information of all products whose sales price is between 1000 and 4000
SELECT * FROM product WHERE sale_price BETWEEN 1000 AND 4000;
5. Find all the products whose sales price is greater than or equal to 4000 and arrange them in descending order of purchase price
SELECT * FROM product WHERE sale_price>=4000 ORDER BY purchase_price DESC;
5. Delete data
1. Delete the library and run away
DELETE FROM <表名>; 删库跑路
2. Delete data - try it yourself! !
DELETE FROM 表名 WHERE <限制条件>
例如:DELETE FROM product WHERE product_id='0005';