1.员工表
# 1.员工表
# drop table employees;
create table employees
(
employee_id INT primary key,
name VARCHAR(50) not null, -- 员工的名字
job_title VARCHAR(50), -- 员工的职位/职称
department_id int, -- 员工所属部门
hire_date DATE, -- 员工入职日期
salary DECIMAL(10, 2), -- 员工薪资
email VARCHAR(100), -- 员工的电子邮件地址
phone_number VARCHAR(15), -- 员工的联系电话
manager_id INT, -- 直接上级的员工 ID(外键)
created_at TIMESTAMP default current_timestamp, -- 创建员工记录的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp, -- 最后更新时间
foreign key (manager_id) references employees (employee_id),
foreign key (department_id) references departments (department_id)
);
# 员工数据插入
insert into
employees (employee_id, name, job_title, department_id, hire_date, salary, email, phone_number, manager_id)
values
(1, '孙权', '技术部经理', 2, '2020-01-15', 80000.00, 'sunquan@example.com', '123-456-7890', null),
(2, '刘备', '市场部经理', 3, '2019-03-20', 95000.00, 'liubei@example.com', '123-456-7891', null),
(3, '诸葛亮', '财务部经理', 4, '2018-07-30', 105000.00, 'zhugeliang@example.com', '123-456-7892', null),
(4, '袁绍', '人力资源部经理', 1, '2021-02-10', 860000.00, 'zhouyu@example.com', '123-456-7893', null),
(5, '黄盖', '技术部专员', 2, '2017-05-15', 40000.00, 'huanggai@example.com', '123-456-7894', 1),
(6, '郭嘉', '客户服务部经理', 5, '2022-06-01', 95000.00, 'guojia@example.com', '123-456-7895', null),
(7, '张飞', '市场部专员', 3, '2020-09-25', 55000.00, 'zhangfei@example.com', '123-456-7896', 2),
(8, '关羽', '市场部专员', 3, '2021-11-10', 50000.00, 'guanyu@example.com', '123-456-7897', 2),
(9, '黄忠', '市场部专员', 3, '2019-04-15', 55000.00, 'huangzhong@example.com', '123-456-7898', 2),
(10, '周瑜', '技术部组长', 2, '2016-08-22', 70000.00, 'zhouyu@example.com', '123-456-7899', 1);
2.部门表
# 2.部门表
# drop table departments;
create table departments
(
department_id INT primary key,
department_name VARCHAR(100) not null, -- 部门名称
location VARCHAR(100), -- 部门的办公地点
manager_id INT, -- 外键,指向员工表中的
created_at TIMESTAMP default current_timestamp, -- 记录创建部门信息的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp -- 记录部门信息最后更新时间
-- foreign key (manager_id) references employees (employee_id)
);
# 部门表数据插入
insert into
departments (department_id, department_name, location, manager_id)
values
(1, '人力资源部', '总部', null),
(2, '技术部', '研发楼', 1), -- 假设员工ID 1是技术部经理
(3, '市场部', '市场中心', 2), -- 假设员工ID 2是市场部经理
(4, '财务部', '财务楼', 3), -- 假设员工ID 3是财务部经理
(5, '客户服务部', '客服中心', null);
3.部门经理表
# 3.部门经理表
# drop table managers;
create table managers
(
manager_id INT primary key,
employee_id INT not null, -- 外键,指向员工表中的
department_id INT, -- 外键,指向部门表中的
hire_date DATE, -- 管理者入职日期
created_at TIMESTAMP default current_timestamp, -- 记录创建管理者信息的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp, -- 记录管理者信息最后更新时间
foreign key (employee_id) references employees (employee_id),
foreign key (department_id) references departments (department_id)
);
# 数据插入
insert into
managers (manager_id, employee_id, department_id, hire_date)
values
(1, 1, 1, '2018-01-15'), -- 员工ID 1是技术部的经理
(2, 2, 3, '2019-03-20'), -- 员工ID 2是市场部的经理
(3, 3, 4, '2020-06-01'), -- 员工ID 3是财务部经理
(4, 4, 1, '2020-06-01'), -- 员工ID 4是财务部人力资源部经理
(5, 6, 5, '2020-06-01'); -- 员工ID 6是客户服务部经理
4.客户表
# 4.客户表
# drop table customers;
create table customers
(
customer_id INT auto_increment primary key,
name VARCHAR(50) not null, -- 客户的名
email VARCHAR(100) not null unique, -- 客户的电子邮件地址
phone_number VARCHAR(15), -- 客户的联系电话
address VARCHAR(255), -- 客户的地址
registration_date DATE, -- 客户注册日期
last_purchase_date DATE, -- 最近购买日期
status ENUM ('active', 'inactive', 'potential') default 'active', -- 客户状态
created_at TIMESTAMP default current_timestamp, -- 记录创建客户信息的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp -- 记录客户信息最后更新时间
);
# 插入数据
insert into
customers (name, email, phone_number, address, registration_date, last_purchase_date, status)
values
('小乔', 'xiaoqiao@example.com', '123-456-7890', '扬州庐江皖县今安徽潜山', '2023-01-10', '2024-11-10', 'active'),
('大乔', 'daqiao@example.com', '123-456-7891', '扬州庐江皖县今安徽潜山, USA', '2022-05-20', null, 'inactive'),
('貂蝉', 'diaochan@example.com', '123-456-7892', '山西省忻州市东南三公里的木芝村', '2024-03-15', '2024-10-15',
'potential');
5.产品表
# 5.产品表
# drop table products;
create table products
(
product_id INT auto_increment primary key,
product_name VARCHAR(100) not null, -- 产品名称
category_id INT, -- 外键,指向产品类别表中
price DECIMAL(10, 2) not null, -- 产品价格
quantity_in_stock INT default 0, -- 当前库存数量
description TEXT, -- 产品描述
created_at TIMESTAMP default current_timestamp, -- 记录创建产品信息的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp, -- 记录产品信息最后更新时间
status ENUM ('available', 'discontinued', 'out_of_stock') default 'available',
foreign key (category_id) references categories (category_id)
);
# 插入数据
insert into
products (product_name, category_id, price, quantity_in_stock, description, status)
values
('笔记本电脑', 1, 999.99, 50, '专为专业人士设计的高性能笔记本电脑。', 'available'),
('智能手机', 1, 699.99, 100, '采用尖端技术的最新智能手机。', 'available'),
('无线鼠标', 1, 29.99, 150, '符合人体工程学的无线鼠标,舒适。', 'available'),
('监视器', 1, 199.99, 30, '27英寸高分辨率显示器。', 'out_of_stock');
6.商品类别表
# 6.商品类别表
# drop table categories;
create table categories
(
category_id INT primary key,
category_name VARCHAR(100) not null unique, -- 类别名称
description TEXT, -- 类别描述
created_at TIMESTAMP default current_timestamp, -- 记录创建类别信息的时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp -- 记录类别信息最后更新时间
);
# 数据插入
insert into
categories (category_id, category_name, description)
values
(1, '数码产品', '电子工厂设备和小工具,包括电脑、智能手机和配件。'),
(2, '家具', '家居和办公家具,如椅子、桌子和书桌。'),
(3, '衣服', '男士、女士和儿童服装。'),
(4, '书', '各种类型的印刷和数字书籍。');
7.订单表
# 7.订单表
# drop table orders;
create table orders
(
order_id INT auto_increment primary key,
customer_id INT not null, -- 存储与此订单相关的客户ID
order_date DATETIME default current_timestamp, -- 自动记录订单创建的日期和时间,
status ENUM ('Pending', 'Completed', 'Cancelled', 'Shipped') not null, -- 表示订单的状态:Pending待定的、Completed完成、Cancelled取消、Shipped运输
total_amount DECIMAL(10, 2) not null, -- 存储订单的总金额
shipping_address VARCHAR(255) not null, -- 存储订单的收货地址信息
payment_method VARCHAR(50) not null, -- 存储客户选择的支付方式
tracking_number VARCHAR(50), -- 用于存储运输服务提供的追踪编号
created_at TIMESTAMP default current_timestamp, -- 自动记录订单信息的创建时间
updated_at TIMESTAMP default current_timestamp on update current_timestamp, -- 自动记录订单信息的最后更新时间
foreign key (customer_id) references customers (customer_id)
);
# 数据插入
insert into
orders (customer_id, status, total_amount, shipping_address, payment_method)
values
(1, 'Pending', 1499.99, '广西南宁', 'Credit Card'),
(2, 'Completed', 299.99, '广西桂林', 'PayPal'),
(3, 'Shipped', 799.99, '广西柳州', 'Debit Card');