-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationships.sql
More file actions
102 lines (59 loc) · 2.85 KB
/
relationships.sql
File metadata and controls
102 lines (59 loc) · 2.85 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
USE employees;
SHOW INDEXES FROM employees;
SHOW INDEXES FROM salaries;
SELECT emp_no, salary FROM salaries WHERE salary BETWEEN 60000 AND 62000;
# to create an index
# ALTER TABLE table_name ADD INDEX index_name (column_name);
ALTER TABLE salaries ADD INDEX salary_index (salary);
DROP INDEX salary_index ON salaries;
# syntax for unique index
# ALTER TABLE table_name ADD UNIQUE (column_names);
USE codeup_test_db;
SHOW TABLES;
SHOW CREATE TABLE quotes;
CREATE TABLE `quotes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` varchar(75) NOT NULL,
`content` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE quotes ADD UNIQUE (content);
INSERT INTO quotes (content, author) VALUES ('Why do astronauts use Linux? They can\'t open Windows in space', 'THE Ryan Murray');
# This won't work now. Javier can't steal Ryan's joke
INSERT INTO quotes (content, author) VALUES ('Why do astronauts use Linux? They can\'t open Windows in space', 'Javier Ruedas');
USE employees;
SHOW TABLES;
DESCRIBE employees;
DESCRIBE titles;
DESCRIBE salaries;
DESCRIBE departments;
DESCRIBE dept_emp;
DESCRIBE dept_manager;
USE codeup_test_db;
SHOW INDEXES FROM albums;
DROP INDEX artist ON albums;
ALTER TABLE albums ADD UNIQUE unique_artist_and_album_combo (artist, name);
INSERT INTO albums (artist, name) VALUES ('Adele', '21');
USE employees;
SELECT employees.last_name, salaries.salary FROM employees JOIN salaries ON employees.emp_no = salaries.emp_no LIMIT 100;
USE join_test_db;
SELECT users.name AS user, roles.name AS role FROM users JOIN roles ON roles.id = users.role_id;
# Here, users is the "left" table -- meaning, on the left hand of the JOIN
# MySQL takes ALL the rows from the left table
# and matches them to rows on the right table
SELECT users.name AS user, roles.name AS role FROM users LEFT JOIN roles ON roles.id = users.role_id;
# RIGHT JOIN version
SELECT users.name AS user, roles.name AS role FROM roles RIGHT JOIN users ON roles.id = users.role_id;
USE codeup_test_db;
SELECT first_name, albums.name FROM persons JOIN preferences ON persons.id = preferences.person_id JOIN albums ON preferences.album_id = albums.id;
USE employees;
SELECT CONCAT(employees.first_name, ' ', employees.last_name) AS name, salary FROM employees JOIN salaries ON employees.emp_no = salaries.emp_no WHERE salaries.to_date LIKE '9%' LIMIT 200;
# department managers' birth days : subqueries style
SELECT CONCAT(first_name, ' ', last_name), birth_date
FROM employees
WHERE emp_no IN (
SELECT emp_no FROM dept_manager
);
# department managers' birth days : join style
SELECT CONCAT(first_name, ' ', last_name), birth_date
FROM employees JOIN dept_manager on employees.emp_no = dept_manager.emp_no;