将其他表和员工表进行主键关联,实现一张表结构
时间: 2024-01-30 11:02:20 浏览: 68
好的,以下是将其他表和员工表进行主键关联的代码,实现一张表结构:
```
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
phone VARCHAR(20),
email VARCHAR(100),
hire_date DATE,
dept_id INT,
job_id INT,
salary DECIMAL(10,2),
check_in DATETIME,
check_out DATETIME,
CONSTRAINT FK_employee_dept FOREIGN KEY (dept_id) REFERENCES department(id),
CONSTRAINT FK_employee_job FOREIGN KEY (job_id) REFERENCES job(id)
);
CREATE TABLE department (
id INT PRIMARY KEY,
name VARCHAR(50),
manager_id INT,
CONSTRAINT FK_department_manager FOREIGN KEY (manager_id) REFERENCES employee(id)
);
CREATE TABLE job (
id INT PRIMARY KEY,
title VARCHAR(50),
description VARCHAR(200),
salary DECIMAL(10,2)
);
CREATE TABLE salary (
employee_id INT PRIMARY KEY,
pay_date DATE,
CONSTRAINT FK_salary_employee FOREIGN KEY (employee_id) REFERENCES employee(id)
);
CREATE TABLE attendance (
employee_id INT PRIMARY KEY,
check_in DATETIME,
check_out DATETIME,
CONSTRAINT FK_attendance_employee FOREIGN KEY (employee_id) REFERENCES employee(id)
);
CREATE TABLE admin (
id INT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50),
email VARCHAR(100),
phone VARCHAR(20)
);
```
这样,我们将员工表、部门表、职位表、工资表和考勤表的字段都放到了员工表中,并且使用主键关联的方式与其他表进行了关联。这样可以避免表之间的数据冗余和错误,也方便了数据的查询和管理。同时,这样的表结构也符合第三范式的要求。
阅读全文