用sql写一个网上书店系统
时间: 2023-12-26 10:06:51 浏览: 48
假设我们需要建立一个网上书店系统,我们可以使用以下SQL语句来创建数据库和表:
创建数据库:
```
CREATE DATABASE bookstore;
```
创建书籍表:
```
CREATE TABLE books (
book_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL
);
```
创建订单表:
```
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(255) NOT NULL,
order_date DATE NOT NULL,
total DECIMAL(10,2) NOT NULL
);
```
创建订单详情表:
```
CREATE TABLE order_details (
order_id INT,
book_id INT,
quantity INT NOT NULL,
price DECIMAL(10,2) NOT NULL,
PRIMARY KEY (order_id, book_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (book_id) REFERENCES books(book_id)
);
```
然后我们可以向书籍表中插入一些数据:
```
INSERT INTO books (title, author, price) VALUES
('The Great Gatsby', 'F. Scott Fitzgerald', 12.99),
('To Kill a Mockingbird', 'Harper Lee', 10.99),
('1984', 'George Orwell', 8.99),
('Pride and Prejudice', 'Jane Austen', 7.99);
```
最后,如果我们想查询某个作者的所有书籍,我们可以使用以下语句:
```
SELECT * FROM books WHERE author = 'F. Scott Fitzgerald';
```
阅读全文