MySQL同时创建10个表代码
时间: 2024-03-01 14:49:49 浏览: 58
mysql批量建表
好的,下面是一个同时创建10个表的示例代码:
```sql
CREATE TABLE departments (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE employees (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
hire_date DATE NOT NULL,
job_title VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL,
department_id INT(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (department_id) REFERENCES departments(id)
);
CREATE TABLE projects (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
budget DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tasks (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
due_date DATE NOT NULL,
status VARCHAR(20) NOT NULL,
project_id INT(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (project_id) REFERENCES projects(id)
);
CREATE TABLE students (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
birth_date DATE NOT NULL,
address VARCHAR(100) NOT NULL,
major VARCHAR(50) NOT NULL,
gpa DECIMAL(3,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE courses (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
description VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE enrollments (
id INT(11) NOT NULL AUTO_INCREMENT,
student_id INT(11) NOT NULL,
course_id INT(11) NOT NULL,
grade VARCHAR(2) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (course_id) REFERENCES courses(id)
);
CREATE TABLE orders (
id INT(11) NOT NULL AUTO_INCREMENT,
customer_name VARCHAR(50) NOT NULL,
order_date DATE NOT NULL,
total DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE order_items (
id INT(11) NOT NULL AUTO_INCREMENT,
product_name VARCHAR(50) NOT NULL,
quantity INT(11) NOT NULL,
price DECIMAL(10,2) NOT NULL,
order_id INT(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (order_id) REFERENCES orders(id)
);
CREATE TABLE books (
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
author VARCHAR(50) NOT NULL,
publisher VARCHAR(50) NOT NULL,
publish_date DATE NOT NULL,
genre VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
以上是一个简单的示例,你可以根据自己的需求修改字段名称和数据类型来创建更多的表。同时,你还需要考虑表之间的关系,以便在需要时进行查询。
阅读全文