-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact_Management_System.sql
148 lines (118 loc) · 3.62 KB
/
Contact_Management_System.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
create database contact_management_sys;
use contact_management_sys;
-- Creating table for admins
CREATE TABLE admins (
admin_id INT AUTO_INCREMENT PRIMARY KEY,
admin_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
INSERT INTO admins (admin_name, email, password)
VALUES
('Admin 1', '[email protected]', 'password1'),
('Admin 2', '[email protected]', 'password2'),
('Admin 3', '[email protected]', 'password3'),
('Admin 4', '[email protected]', 'password4'),
('Admin 5', '[email protected]', 'password5');
-- Creating table for users
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
INSERT INTO users (username, email, password)
VALUES
('user1', '[email protected]', 'userpassword1'),
('user2', '[email protected]', 'userpassword2'),
('user3', '[email protected]', 'userpassword3'),
('user4', '[email protected]', 'userpassword4'),
('user5', '[email protected]', 'userpassword5');
-- Creating table for contacts
CREATE TABLE contacts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
phone VARCHAR(15),
address TEXT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
INSERT INTO contacts (user_id, name, email, phone, address)
VALUES
(1, 'Contact 1', '[email protected]', '1234567890', '123 Main St, City'),
(2, 'Contact 2', '[email protected]', '2345678901', '456 Elm St, Town'),
(3, 'Contact 3', '[email protected]', '3456789012', '789 Oak St, Village'),
(4, 'Contact 4', '[email protected]', '4567890123', '987 Pine St, County'),
(5, 'Contact 5', '[email protected]', '5678901234', '654 Maple St, District');
-- Creating table for logs
CREATE TABLE logs (
id INT AUTO_INCREMENT PRIMARY KEY,
action VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
INSERT INTO logs (action, user_id)
VALUES
('Login', 1),
('Logout', 2),
('Login', 3),
('Logout', 4),
('Login', 5);
select * from admins;
select * from users;
select * from contacts;
select * from logs;
-- query1
SELECT c.name, c.email, c.phone, c.address
FROM contacts c
JOIN users u ON c.user_id = u.id
WHERE u.username = 'user1';
-- query2
UPDATE users
SET password = 'newSecurePassword123'
WHERE username = 'user3';
-- query3
SELECT *
FROM logs
WHERE action = 'Login';
-- query4
DELETE FROM contacts
WHERE email = '[email protected]';
-- creating views
-- view1
CREATE VIEW UserContacts AS
SELECT u.username, u.email AS user_email, c.name AS contact_name, c.email AS contact_email, c.phone
FROM users u
JOIN contacts c ON u.id = c.user_id;
-- view2
CREATE VIEW UserLoginLogs AS
SELECT u.username, l.action, l.created_at
FROM users u
JOIN logs l ON u.id = l.user_id
WHERE l.action = 'Login';
-- view3
CREATE VIEW RecentUserActivity AS
SELECT u.username, l.action, l.created_at
FROM users u
JOIN logs l ON u.id = l.user_id
ORDER BY l.created_at DESC;
-- view4
CREATE VIEW UsersMultipleContacts AS
SELECT u.username, COUNT(c.id) AS contact_count
FROM users u
JOIN contacts c ON u.id = c.user_id
GROUP BY u.id
HAVING COUNT(c.id) > 1;
-- view5
CREATE VIEW AdminDetails AS
SELECT admin_name, email,
CASE WHEN password IS NOT NULL THEN 'Set' ELSE 'Not Set' END AS password_status
FROM admins;
-- displaying views
SELECT * FROM UserContacts;
SELECT * FROM UserLoginLogs;
SELECT * FROM RecentUserActivity;
SELECT * FROM UsersMultipleContacts;
SELECT * FROM AdminDetails;