Skip to content

Commit fcf941b

Browse files
committed
Create query.sql
1 parent 8152ace commit fcf941b

File tree

28 files changed

+244
-35
lines changed
  • Introduction to Relational Databases in SQL
    • 01 Your first database
      • 02 Query information_schema with SELECT
      • 03 CREATE your first few TABLEs
      • 04 ADD a COLUMN with ALTER TABLE
      • 05 RENAME and DROP COLUMNs in affiliations
      • 06 Migrate data with INSERT INTO SELECT DISTINCT
      • 07 Delete tables with DROP TABLE
    • 02 Enforce data consistency with attribute constraints
    • 03 Uniquely identify records with key constraints
      • 01 Get to know SELECT COUNT DISTINCT
      • 02 Identify keys with SELECT COUNT DISTINCT
      • 04 ADD key CONSTRAINTs to the tables
      • 05 Add a SERIAL surrogate key
      • 06 CONCATenate columns to a surrogate key
      • 07 Test your knowledge before advancing
    • 04 Glue together tables with foreign keys
      • 01 REFERENCE a table with a FOREIGN KEY
      • 02 Explore foreign key constraints
      • 03 JOIN tables linked by a foreign key
      • 04 Add foreign keys to the affiliations table
      • 05 Populate the professor_id column
      • 06 Drop firstname and lastname
      • 08 Change the referential integrity behavior of a key
      • 09 Count affiliations per university
      • 10 Join all the tables together

28 files changed

+244
-35
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Query the right table in information_schema
2+
SELECT table_name
3+
FROM information_schema.tables
4+
-- Specify the correct table_schema value
5+
WHERE table_schema = 'public';
6+
7+
-- Query the right table in information_schema to get columns
8+
SELECT column_name, data_type
9+
FROM information_schema.columns
10+
WHERE table_name = 'university_professors' AND table_schema = 'public';
11+
12+
-- Query the first five rows of our table
13+
SELECT *
14+
FROM university_professors
15+
LIMIT 5;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Create a table for the professors entity type
2+
CREATE TABLE professors (
3+
firstname text,
4+
lastname text
5+
);
6+
7+
-- Print the contents of this table
8+
SELECT *
9+
FROM professors
10+
11+
-- Create a table for the universities entity type
12+
CREATE TABLE universities (
13+
university_shortname text,
14+
university text,
15+
university_city text
16+
);
17+
18+
-- Print the contents of this table
19+
SELECT *
20+
FROM universities
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Add the university_shortname column
2+
ALTER TABLE professors
3+
ADD COLUMN university_shortname text;
4+
5+
-- Print the contents of this table
6+
SELECT *
7+
FROM professors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Rename the organisation column
2+
ALTER TABLE affiliations
3+
RENAME COLUMN organisation TO organization;
4+
5+
-- Delete the university_shortname column
6+
ALTER TABLE affiliations
7+
DROP COLUMN university_shortname;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Insert unique professors into the new table
2+
INSERT INTO professors
3+
SELECT DISTINCT firstname, lastname, university_shortname
4+
FROM university_professors;
5+
6+
-- Doublecheck the contents of professors
7+
SELECT *
8+
FROM professors;
9+
10+
-- Insert unique affiliations into the new table
11+
INSERT INTO affiliations
12+
SELECT DISTINCT firstname, lastname, function, organization
13+
FROM university_professors;
14+
15+
-- Doublecheck the contents of affiliations
16+
SELECT *
17+
FROM affiliations;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Delete the university_professors table
2+
DROP TABLE university_professors;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Types of database constraints
2+
3+
Which of the following is not used to enforce a database constraint?
4+
5+
**Possible Answers**
6+
7+
* Foreign keys
8+
* SQL aggregate functions
9+
* The BIGINT data type
10+
* Primary keys
11+
12+
**Answer**
13+
14+
> SQL aggregate functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Let's add a record to the table
2+
INSERT INTO transactions (transaction_date, amount, fee)
3+
VALUES ('2018-09-24', 5454, '30');
4+
5+
-- Doublecheck the contents
6+
SELECT *
7+
FROM transactions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Select the university_shortname column
2+
SELECT DISTINCT(university_shortname)
3+
FROM professors;
4+
5+
-- Specify the correct fixed-length character type
6+
ALTER TABLE professors
7+
ALTER COLUMN university_shortname
8+
TYPE char(3);
9+
10+
-- Change the type of firstname
11+
ALTER TABLE professors
12+
ALTER COLUMN firstname
13+
TYPE varchar(64);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Convert the values in firstname to a max. of 16 characters
2+
ALTER TABLE professors
3+
ALTER COLUMN firstname
4+
TYPE varchar(16)
5+
USING SUBSTRING(firstname FROM 1 FOR 16)

0 commit comments

Comments
 (0)