-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_create_tables.sql
More file actions
134 lines (120 loc) · 5.04 KB
/
01_create_tables.sql
File metadata and controls
134 lines (120 loc) · 5.04 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
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
/*
===========================================================================================
Script Name: Script2_1_Create
Authors: Dimitrios Chasanidis, Konstantina Tzeikou, Panteleimon Skenderoglou, Thomas Kanoutas
Date: 22-01-2025
Description:
This script creates the database schema for apartment rental system, including
tables, sequences, and initial session settings. The schema consists of five main
entities (OWNERS, APARTMENTS, TENANTS, RENTAL`, and REVIEWS) and the relationships
between them. Additionally, it configures the session to handle date formatting
and language settings.
===========================================================================================
Script Purpose:
1) Configure the database session with appropriate date format and language.
2) Define the following database objects:
- Tables to store data about owners, apartments, tenants, rentals, and reviews.
- Sequences to auto-generate unique IDs for primary key columns.
===========================================================================================
Database Objects Created:
1) Tables:
- OWNERS: Stores owner details (e.g., ID, name, address, and TIN).
- APARTMENTS: Stores apartment details (e.g., address, owner, price, and floor).
- TENANTS: Stores tenant details (e.g., name, gender, and country).
- RENTALS: Stores rental information (e.g., start and end dates).
- REVIEWS: Stores reviews for apartments (e.g., star rating and comments).
2) Sequences:
- `seq_owners`: Generates IDs for the `OWNERS` table.
- `seq_apartments`: Generates IDs for the `APARTMENTS` table.
- `seq_tenants`: Generates IDs for the `TENANTS` table.
- `seq_rentals`: Generates IDs for the `RENTALS` table.
- `seq_reviews`: Generates IDs for the `REVIEWS` table.
===========================================================================================
*/
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY' NLS_DATE_LANGUAGE='english';
select sysdate from dual;
--Create OWNERS table
CREATE TABLE owners (
O_ID NUMBER(2), -- Owner's ID
O_NAME VARCHAR2(30) NOT NULL, -- Owner's first name
O_SURNAME VARCHAR2(30) NOT NULL, --Owner's last name
EMAIL VARCHAR2(50) NOT NULL, -- Owner's email
CITY VARCHAR2(30) NOT NULL, -- city where the owner lives
D_OF_BIRTH DATE NOT NULL, -- Owner's date of birth
TIN NUMBER(9) NOT NULL, -- Owner's Tax Identification Number
O_ADR VARCHAR2(60) NOT NULL -- Owner's address
);
-- Create sequence for OWNERS table
CREATE SEQUENCE seq_owners
INCREMENT BY 1
START WITH 1
NOCYCLE
NOCACHE;
---------------------------------
-- Create APARTMENTS table
CREATE TABLE apartments (
APT_ID NUMBER(2), -- Apartment's ID
O_ID NUMBER(2), -- Owner's ID
STREET VARCHAR2(30) NOT NULL, -- Apartment's street
STR_NUMBER NUMBER(2) NOT NULL, -- Apartment's street number
POSTAL_CODE NUMBER(5) NOT NULL, -- Apartment's Postal Code
CITY VARCHAR2(30) DEFAULT 'ATHENS' NOT NULL, -- The city of the Apartment
FLOOR NUMBER NOT NULL, -- Apartment's floor (0 for ground, 1 for first floor etc.)
PRICE NUMBER(4,2) NOT NULL -- Apartment's price per day (in euros)
);
-- Create sequence for APARTMENTS table
CREATE SEQUENCE seq_apartments
INCREMENT BY 1
START WITH 1
NOCYCLE
NOCACHE;
---------------------------------
-- Create TENANTS table
CREATE TABLE tenants (
T_ID NUMBER(2), -- Tenant's ID
T_NAME VARCHAR2(30) NOT NULL, -- Tenant's first name
T_SURNAME VARCHAR2(30) NOT NULL, -- Tenant's last name
T_GENDER CHAR(1) NOT NULL, -- Tenant's gender (female, male, other)
EMAIL VARCHAR2(50) NOT NULL, -- Tenant's email
D_OF_BIRTH DATE NOT NULL, -- Tenant's date of birth
COUNTRY VARCHAR2(30) DEFAULT 'GREECE', -- Tenant's country of origin
N_OF_TENANTS NUMBER(2) DEFAULT 1 -- Number of tenants (how many people are staying in the apartment)
);
-- Create sequence for TENANTS table
CREATE SEQUENCE seq_tenants
INCREMENT BY 1
START WITH 1
NOCYCLE
NOCACHE;
---------------------------------
-- Create RENTALS table
CREATE TABLE rentals (
RENTAL_ID NUMBER(2), -- Rental's ID
APT_ID NUMBER(2), -- Apartment's ID
T_ID NUMBER(2), -- Tenant's ID
START_DATE DATE NOT NULL, -- Start date of the Rental
END_DATE DATE NOT NULL -- End date of the Rental
);
-- Create sequence for RENTALS table
CREATE SEQUENCE seq_rentals
INCREMENT BY 1
START WITH 1
NOCYCLE
NOCACHE;
---------------------------------
-- Create REVIEWS table
CREATE TABLE reviews (
REVIEW_ID NUMBER(2), -- Review's ID
APT_ID NUMBER(2), -- Apartment's ID
T_ID NUMBER(2), -- Tenant's ID
REVIEW_DATE DATE NOT NULL, -- Date of the Review
STAR_RATING NUMBER(1) NOT NULL, -- Star rating
REVIEW_TXT VARCHAR(4000) NOT NULL -- Review's text
);
-- Create sequence for reviews table
CREATE SEQUENCE seq_reviews
INCREMENT BY 1
START WITH 1
NOCYCLE
NOCACHE;
---------------------------------