-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.sql
74 lines (74 loc) · 2.78 KB
/
query.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
-- Drop tables if they exist and their dependent objects
DROP TABLE IF EXISTS orders CASCADE;
DROP TABLE IF EXISTS customers CASCADE;
-- Create the 'customers' table
CREATE TABLE orders (
order_id SERIAL NOT NULL,
customer_id INTEGER,
date_ordered DATE,
date_delivered DATE,
price REAL NOT NULL,
units INTEGER DEFAULT 1,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
-- Create the 'orders' table with a foreign key reference to 'customers'
CREATE TABLE customers (
customer_id SERIAL NOT NULL,
first_name VARCHAR(20),
last_name VARCHAR(20),
email VARCHAR(30) NOT NULL,
PRIMARY KEY (customer_id),
UNIQUE (email)
);
-- Insert values into the 'customers' table
INSERT INTO customers VALUES
(0, 'zoe', 'le', '[email protected]'),
(1, 'max', 'sivolella', '[email protected]'),
(2, 'varsha', 'moturi', '[email protected]'),
(3, 'robert', 'clements', '[email protected]'),
(4, 'jeff', 'bezos', '[email protected]');
-- Insert values into the 'orders' table
INSERT INTO orders VALUES
(0, 1, '2023-09-15', '2023-10-17', 300.0, 2),
(1, 0, '2023-02-17', '2023-02-20', 50.0, 1),
(2, 3, '2022-05-16', '2022-05-25', 100.0, 3);
-- Attempt to insert a duplicate record into the 'customers' table
-- This will fail due to the primary key constraint on the 'customer_id' column
INSERT INTO customers VALUES(4, 'elon', 'musk', '[email protected]');
INSERT INTO customers VALUES(6, 'elon', 'musk', '[email protected]');
-- Selecting email from the 'customers' table using a subquery and foreign key
SELECT email
FROM customers
WHERE customer_id = (SELECT customer_id FROM orders WHERE order_id = 2);
-- Attempt to delete records from the 'customers' table
-- This will fail because it has a constraint with orders table
DELETE FROM customers WHERE customer_id=3;
-- Attempt to insert another duplicate record with the same email
-- This will fail due to the unique constraint on the 'email' column
INSERT INTO customers VALUES (7, NULL, NULL, '[email protected]');
-- Add a unique constraint to the 'phone_number' column in the 'customers' table
ALTER TABLE customers ADD phone_number VARCHAR(13) UNIQUE;
-- Update phone numbers for specific customer records
UPDATE customers SET phone_number = '803-456-2342' WHERE customer_id=0;
UPDATE customers SET phone_number = '567-345-6753' WHERE customer_id=1;
UPDATE customers SET phone_number = '123-673-9876' WHERE customer_id=2;
-- This will also fail due to the unique constraint on the 'phone_number' column
UPDATE customers SET phone_number = '803-456-2342' WHERE customer_id=3;
-- Select all records from the 'customers' table
SELECT * FROM customers;
-- Select all records from the 'orders' table
SELECT * FROM orders;