-
Notifications
You must be signed in to change notification settings - Fork 0
/
creating_tables.sql
74 lines (62 loc) · 1.59 KB
/
creating_tables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- Create a new table - Departments
CREATE TABLE departments (
dept_no CHAR(5) PRIMARY KEY,
dept_name VARCHAR NOT NULL
);
-- Query all fields from the table
SELECT *
FROM departments;
-- Query only the `dept_name` field
SELECT dept_name
FROM departments;
-- Create a new table - Titles
CREATE TABLE titles (
title_id CHAR (5) PRIMARY KEY,
title VARCHAR NOT NULL
);
-- Query all fields from the table
SELECT *
FROM titles;
-- Create a new table - Employees
CREATE TABLE employees (
emp_no INTEGER PRIMARY KEY,
emp_title CHAR(5) NOT NULL,
FOREIGN KEY (emp_title) REFERENCES titles(title_id),
birth_date date,
first_name VARCHAR NOT NULL,
last_name VARCHAR NOT NULL,
sex VARCHAR(5),
hire_date date
);
-- Query all fields from the table
SELECT *
FROM employees;
-- Create a new table - Department Employees
CREATE TABLE dept_employees (
emp_no INTEGER NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees(emp_no),
dept_no CHAR(5) NOT NULL,
FOREIGN KEY (dept_no) REFERENCES departments(dept_no)
);
-- Query all fields from the table
SELECT *
FROM dept_employees;
-- Create a new table - Department Manager
CREATE TABLE dept_manager (
dept_no CHAR(5) NOT NULL,
FOREIGN KEY (dept_no) REFERENCES departments(dept_no),
emp_no INTEGER NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees(emp_no)
);
-- Query all fields from the table
SELECT *
FROM dept_manager;
-- Create a new table - Salaries
CREATE TABLE salaries (
emp_no INTEGER NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees(emp_no),
salary INTEGER NOT NULL
);
-- Query all fields from the table
SELECT *
FROM salaries;