forked from thatsabhishek/Coding_Ninjas_DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlquery4.sql
70 lines (56 loc) · 2.27 KB
/
sqlquery4.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
Problem Statement:
Consider the tables given below:
1. The table users contains features like id, full_name, enabled, last_login. The attribute id here will be the primary key.
Attribute List(s):
Attribute | Data Type
id | INT (Primary Key)
full_name | VARCHAR
enabled | CHAR
last_login | DATE
2.The table addresses contains features like user_id, street, city, state. The user_id here will be the primary key as well as foreign key that refers to id(users table). The attributes street, city and state should be declared NOT NULL.
Attribute List(s):
Attribute | Data Type
user_id | INT (Primary Key)
street | VARCHAR
city | VARCHAR
state | VARCHAR
Formulate a SQL query to create these tables with all the constraints given along with the table. Also, add constraints to correlate these tables.
Print the Table Schema for each table created as follows:
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_name = 'your_table';
Note - 1: First print table users and then addresses. Position the above command just after the CREATE TABLE command for each table.
Note - 2: Replace 'your_table' with the actual table name used in the query.
Solution:
CREATE TABLE users(
id INT PRIMARY KEY,
full_name VARCHAR(20) NOT NULL,
enabled CHAR(10) NOT NULL,
last_login DATE );
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_name = 'users';
CREATE TABLE addresses(
user_id INT PRIMARY KEY,
street VARCHAR(50) NOT NULL,
city VARCHAR(25) NOT NULL,
state VARCHAR(50) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id));
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_name = 'addresses';
Output:
CREATE TABLE
table_name | column_name | data_type
------------+-------------+-------------------
users | id | integer
users | full_name | character varying
users | enabled | character
users | last_login | date
CREATE TABLE
table_name | column_name | data_type
------------+-------------+-------------------
addresses | user_id | integer
addresses | street | character varying
addresses | city | character varying
addresses | state | character varying