请用sql语句写一套超市管理系统
时间: 2023-06-28 15:12:18 浏览: 68
好的,以下是一个基本的超市管理系统的SQL语句:
创建表格:
```
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price FLOAT NOT NULL,
stock INT NOT NULL,
category VARCHAR(255) NOT NULL
);
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
date_ordered DATE NOT NULL
);
CREATE TABLE order_items (
id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
添加产品:
```
INSERT INTO products (name, price, stock, category)
VALUES ('Toothbrush', 1.99, 100, 'Personal Care');
INSERT INTO products (name, price, stock, category)
VALUES ('Shampoo', 4.99, 50, 'Personal Care');
INSERT INTO products (name, price, stock, category)
VALUES ('Bread', 2.49, 75, 'Food');
INSERT INTO products (name, price, stock, category)
VALUES ('Milk', 3.99, 25, 'Food');
```
查询产品:
```
SELECT * FROM products;
SELECT name, price, stock FROM products WHERE category = 'Personal Care';
```
更新产品:
```
UPDATE products SET stock = 80 WHERE name = 'Bread';
```
添加订单:
```
INSERT INTO orders (customer_name, date_ordered)
VALUES ('John Doe', '2021-01-01');
INSERT INTO orders (customer_name, date_ordered)
VALUES ('Jane Smith', '2021-01-02');
```
添加订单项:
```
INSERT INTO order_items (order_id, product_id, quantity)
VALUES (1, 3, 2);
INSERT INTO order_items (order_id, product_id, quantity)
VALUES (2, 1, 1);
```
查询订单:
```
SELECT * FROM orders;
SELECT order_items.quantity, products.name, products.price
FROM order_items
JOIN products ON order_items.product_id = products.id
WHERE order_items.order_id = 1;
```
这只是一个简单的例子,实际的超市管理系统可能需要更多的表格和更复杂的查询。
阅读全文