Getting duplicate data when using the select statements - sql

I'm getting duplicate data when trying to use the select statements, I have 9 rooms but I'm getting around 50-70 rooms when trying to display them. Help please?
I'm trying to insert data and display it using the select statement.
create table gym (
GymName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table Spa (
spaName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table customer (
CustomerID int primary key,
Firstname varchar(200) not null,
LastName varchar(200) not null,
DOB date not null check (DATEDIFF(year,DOB,getdate ()) > 18) ,
Gender char(4) not null check(Gender ='M' or Gender = 'F'),
Address varchar(200) not null default 'Jordan',
spaName VARCHAR(200) foreign key references Spa(spaName),
GymName VARCHAR(200) foreign key references gym(GymName),
);
Create table CustomerPhoNo (
CustomerID int foreign key references customer(CustomerID),
PhoneNo bigint not null,
);
create table Room (
roomNo int primary key,
Availability char(4) not null,
NoOfBeds int not null,
Rate int not null,
CheckIn date,
CheckOut date,
Price Decimal not null,
Breakfast char(4),
CustomerID int foreign key references customer(CustomerID),
);
create table LocationOfRoom (
roomNo int foreign key references Room(roomNo),
seaview char(4),
Location varchar(20) not null,
);
create table RoomType (
roomNo int foreign key references Room(roomNo),
familyRoom char(4),
doubleRoom char(4),
singleRoom char(4),
);
create table Gservice (
GymName VARCHAR(200) foreign key references gym(GymName),
Service VARCHAR(500) not null,
MachineType VARCHAR(500) not null,
);
create table PaymentCard (
CardID int primary key,
issueDate date not null,
Expirydate date not null,
CustomerID int foreign key references customer(CustomerID),
);
insert into customer values (325,'Mohammad','Alasharan','06-04-1984','M','Amman', 'BeautySpa', 'StrongBody')
insert into customer values (348,'John','Shelby','10-18-1998','M','Birmingham', 'LushLife', 'SilverGym')
insert into customer values (495,'Thomas','Hoffman','04-26-1968','M','Johannesburg', 'RelaxationTherapy', 'SilverGym')
insert into customer values (194,'Anne','Frank','07-22-2001','F','Frankfurt', 'BeautySpa', 'StrongBody')
insert into customer values (628,'Katie','Swan','02-10-1997','F','New South Wales', 'LushLife', 'FitnessHeroes')
insert into customer values (246,'Mahmoud','Alkutaifan','04-21-1994','M','Amman', 'BeautySpa', 'FitnessHeroes')
insert into customer values (864,'Karl-Heinz','Rummenigge','09-25-1955','M','Lippstadt', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (824,'Dennis','Law','09-21-1979','M','london', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (463,'Carles','Puyol','06-17-1973','M','madrid', 'LushLife', 'FitnessHeroes')
insert into Room values (124,'yes','1','4',null,null,'30','yes',null)
insert into Room values (135,'no','2','5','05-06-2022','05-09-2022','55','yes',495)
insert into Room values (121,'yes','1','3',null,null,'40','yes',null)
insert into Room values (139,'no','3','4','05-10-2022','05-14-2022','110','no',194)
insert into Room values (131,'no','3','3','05-18-2022','05-22-2022','130','yes',348)
insert into Room values (136,'no','4','4','04-14-2022','04-24-2022','120','yes',194)
insert into Room values (179,'yes','4','5',null,null,'95','no',null)
insert into Room values (138,'no','3','3','04-02-2022','04-06-2022','75','no',246)
insert into Room values (146,'no','3','5','05-10-2022','05-14-2022','80','yes',864)
insert into LocationOfRoom values (124,'no','south')
insert into LocationOfRoom values (135,'yes','north')
insert into LocationOfRoom values (121,'yes','south')
insert into LocationOfRoom values (139,'no','north')
insert into LocationOfRoom values (131,'no','East')
insert into LocationOfRoom values (136,'yes','west')
insert into LocationOfRoom values (179,'no','south')
insert into LocationOfRoom values (138,'no','west')
insert into LocationOfRoom values (146,'yes','north')
insert into RoomType values (124,'no','no','yes')
insert into RoomType values (135,'no','yes','no')
insert into RoomType values (121,'yes','no','no')
insert into RoomType values (139,'no','no','yes')
insert into RoomType values (131,'no','no','yes')
insert into RoomType values (136,'no','no','yes')
insert into RoomType values (179,'no','no','yes')
insert into RoomType values (138,'no','yes','no')
insert into RoomType values (146,'no','no','yes')
-- display Total number of customers who booked a single room with sea view option
select count(Firstname)
from LocationOfRoom, customer, RoomType, Room
where seaview='yes' and singleRoom='yes'
Any help would be greatly appreciated, thank you in advance!

Your FROM clause is missing the join condition for each table. In other words you are getting the cartesian product (every combination of rows) of the tables. Even distinct won't help (it will get the wrong answer). Use join conditions to link the keys of each table to each other. I'll leave this an exercise for you to try out, but this should be enough information to help you out.

I believe the solution for your problem is that you need to use DISTINCT:
SELECT COUNT(DISTINCT(Firstname))
FROM LocationOfRoom, customer, RoomType, Room
WHERE seaview='yes' AND singleRoom='yes'
I have tested it and it retrieves 9 Rooms.

Related

How to write stored procedure in this case?

Database: training
Task: write a stored procedure for transferring a student from one group to another, while checking whether both groups have the same number of disciplines. This is, for example, in one group the number of students was 28, and in the other 20. So that after the procedure, the number was 27 and 21. The code for creating the database tables:...
CREATE DATABASE education
GO
USE education
CREATE TABLE control_type
(
control_type_id INT IDENTITY(1,1) PRIMARY KEY,
control_type_name VARCHAR(20) NOT NULL
);
CREATE TABLE category
(
category_id INT IDENTITY(1,1) PRIMARY KEY,
category_name VARCHAR(20) NOT NULL
);
CREATE TABLE subjects
(
subject_id INT IDENTITY(1,1) PRIMARY KEY,
subject_name VARCHAR(20) NOT NULL,
count_hours INT NOT NULL,
category_id INT NOT NULL
REFERENCES category (category_id)
ON DELETE CASCADE
);
CREATE TABLE groups
(
group_id INT IDENTITY(1,1) PRIMARY KEY,
group_name VARCHAR(20) NOT NULL,
course INT NOT NULL DEFAULT '1',
count_students INT NOT NULL,
count_hours INT NOT NULL
);
CREATE TABLE [session]
(
session_id INT IDENTITY(1,1) PRIMARY KEY,
name_last_prof VARCHAR(20) NOT NULL,
name_1st_prof VARCHAR(20) NOT NULL,
name_2nd_prof VARCHAR(20) NOT NULL,
session_date DATE NOT NULL,
control_type_id INT NOT NULL
REFERENCES control_type (control_type_id)
ON DELETE CASCADE,
group_id INT NOT NULL
REFERENCES groups (group_id)
ON DELETE CASCADE,
subject_id INT NOT NULL
REFERENCES subjects (subject_id)
ON DELETE CASCADE
);
CREATE INDEX idx_group ON groups (count_students);
CREATE INDEX idx_session ON [session] (name_last_prof);
CREATE INDEX idx_subject ON subjects (count_hours);
CREATE INDEX idx_category ON category (category_name);
CREATE INDEX idx_control_type ON control_type (control_type_name);
SET dateformat dmy;
INSERT INTO category VALUES ('�����������');
INSERT INTO category VALUES ('����*������');
INSERT INTO category VALUES ('�����������');
INSERT INTO category VALUES ('��������-���������');
INSERT INTO category VALUES ('���������');
INSERT INTO category VALUES ('��������');
INSERT INTO category VALUES ('������');
INSERT INTO category VALUES ('�������');
INSERT INTO category VALUES ('��������');
INSERT INTO category VALUES ('������');
INSERT INTO control_type VALUES ('����');
INSERT INTO control_type VALUES ('�����');
INSERT INTO subjects VALUES ('��������� ����',80,1);
INSERT INTO subjects VALUES ('��������� ��',75,1);
INSERT INTO subjects VALUES ('������ �������',90,3);
INSERT INTO subjects VALUES ('��������� ����������',100,3);
INSERT INTO subjects VALUES ('������������ �����',120,3);
INSERT INTO subjects VALUES ('���������',110,6);
INSERT INTO subjects VALUES ('��������',100,6);
INSERT INTO subjects VALUES ('�����������',120,6);
INSERT INTO subjects VALUES ('��������',80,6);
INSERT INTO subjects VALUES ('������',100,7);
INSERT INTO subjects VALUES ('������ ������',80,5);
INSERT INTO groups VALUES ('���-01-1',1,20,500);
INSERT INTO groups VALUES ('���-01-2',1,25,520);
INSERT INTO groups VALUES ('ϲ-171',3,23,550);
INSERT INTO groups VALUES ('ϲ-172',3,22,540);
INSERT INTO groups VALUES ('ʲ-181',2,30,490);
INSERT INTO groups VALUES ('ʲ-182',2,29,500);
INSERT INTO groups VALUES ('��-161',4,28,480);
INSERT INTO groups VALUES ('��-162',4,24,510);
INSERT INTO groups VALUES ('��-171',3,26,500);
INSERT INTO groups VALUES ('��-172',3,23,520);
INSERT INTO [session] VALUES ('�����','���','�������','22.06.2022',1,1,3);
INSERT INTO [session] VALUES ('����','����','�������','17.01.22',2,1,4);
INSERT INTO [session] VALUES ('������','³����','�������������','20.06.2022',2,2,3);
INSERT INTO [session] VALUES ('��������','���������','�������������','15.06.2022',2,2,2);
INSERT INTO [session] VALUES ('�����','�����','�������','20.01.22',1,5,8);
INSERT INTO [session] VALUES ('������','���������','����������','18.06.2022',2,6,7);
INSERT INTO [session] VALUES ('�����','�����','³��������','23.01.22',2,3,6);
INSERT INTO [session] VALUES ('�����������','������','��������','16.01.22',2,1,10);
INSERT INTO [session] VALUES ('������','������','����������','13.06.2022',1,2,2);
INSERT INTO [session] VALUES ('������','�����','��������','15.01.22',2,2,4);
I don't understand the logic of writing SQL queries in this case...

How to output a list of all orders placed on day X as calculate the total number of repairs and the total amount spent on them

can you help me to solve this:
Make a list of all orders placed on day X as
calculate the total number of repairs and the total amount spent on them
Thats the code :
CREATE TABLE Employee (
ID INT IDENTITY NOT NULL PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
);
CREATE TABLE Orders (
ID INT IDENTITY NOT NULL PRIMARY KEY,
Date_of_acceptance DATE,
Date_of_deadline DATE,
Status_order NVARCHAR(50) NOT NULL,
Type_of_repair NVARCHAR (50) NOT NULL,
Price DECIMAL (20),
EmployeeID INT,
CustomerID INT,
ItemsID INT
);
ALTER TABLE Orders ALTER COLUMN Price DECIMAL (5,2)
CREATE TABLE Customer (
ID INT IDENTITY NOT NULL PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
PhoneNumber VARCHAR(20)
);
CREATE TABLE Items (
ID INT IDENTITY NOT NULL PRIMARY KEY,
ItemsName NVARCHAR(50) NOT NULL,
Status_items NVARCHAR(50) NOT NULL
);
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Employee FOREIGN KEY (EmployeeID)
REFERENCES Employee(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Customer FOREIGN KEY (CustomerID)
REFERENCES Customer(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Items FOREIGN KEY (ItemsID)
REFERENCES Items(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Printer', 'Accept for the repair ');
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Computer', 'Unclaimed');
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Laptop', 'Accept for the repair');
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Battery' , 'Unclaimed' );
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Computer' , 'Accept for repair' );
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Monitor', 'Unclaimed');
SELECT * FROM Items;
UPDATE Items SET Status_items ='Accept for repair' WHERE ID=7;
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price, EmployeeID, CustomerID, ItemsID)
VALUES('2021-11-16', '2021-11-18','Accept','Broken screen of laptop', 100, 1,1,7);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Broken printer',90,1,1,1);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-5-16', '2021-5-30','Waiting for delivery of part','Rеplacing the motherboard',500,1,1,6);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-8-2', '2021-8-26','Accept','Repair a broken computer',600, 2,2,2);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES('2021-7-12', '2021-7-14','Accept','Change the laptop battery', 120,2,2,5);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Update windows',30,2,2,6);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-2-6', '2021-2-21','Waiting for delivery of part',' Rеplacing the motherboard ',500,3,3,3);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-4-3', '2021-4-25','Accept',' Virus scan',25,3,3,3);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-5-16', '2021-5-30','Accept',' Change the laptop battery',120,3,3,5);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Broken printer',90,3,3,1);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-5', '2021-10-7','Accept','Broken screen',90,3,3,7);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-3-5', '2021-3-8','Accept',' Rеplacing the motherboard',90,3,3,2);
SELECT * FROM Orders;
UPDATE Orders SET Date_of_acceptance ='2021-10-9' WHERE ID=7;
UPDATE Orders SET Date_of_deadline ='2021-10-15' WHERE ID=7;
I did this, but I don't know if it's right
I don't know how to fix it:
SELECT Date_of_deadline,Type_of_repair,Price
FROM Orders
WHERE Orders.Status_order = 'Submitted'(
SELECT SUM(Price) AS 'Sum of repairs'
FROM Orders
WHERE Orders.Status_order = 'Submitted')
I'm confused by your code, you made two separate queries right?
I think you forgot to add a count(1) on the second select as to calculate the total amount of orders and add the date as where parameter to equal it to X

Violation of PRIMARY KEY constraint 'PK__Transact__'. Cannot insert duplicate key in object 'dbo.Transactions_ID'. The duplicate key value is (1001)

When I am trying to insert execute the table and I am getting that error. And when I try to insert more rows into Employee_in I get an error
The number of columns for each row in a table value constructor must be the same
CREATE DATABASE EmployeeDatabase
USE EmployeeDatabase
CREATE TABLE Employee_In
(
EmployeeID INT NOT NULL PRIMARY KEY,
EmployeeName CHAR(30) NOT NULL,
EmployeeCountry CHAR(30)NOT NULL,
EmployeeSalary INT NOT NULL
)
USE EmployeeDatabase
GO
INSERT INTO Employee_In (EmployeeID, EmployeeName, EmployeeCountry, EmployeeSalary)
VALUES (1001, 'Sundar', 'USA', 125000),
(1002, 'Satya', 'CANADA', 120000);
SELECT * FROM Employee_In
CREATE TABLE Transactions_ID
(
TransactionID INT NOT NULL PRIMARY KEY,
EmployeeID INT FOREIGN KEY REFERENCES Employee_In(EmployeeID),
PostalDate VARCHAR(20) NOT NULL,
Amount VARCHAR(40) NOT NULL,
Description CHAR(25),
);
USE EmployeeDatabase
GO
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001','1001','2020-04-07','20000')
Violation of PRIMARY KEY constraint 'PK__Transact__'. Cannot insert duplicate key in object 'dbo.Transactions_ID'. The duplicate key value is (1001)
This means you already have a value 1001 for the PK TransactionID in your table, most likely from and earlier insert.
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001', '1001', '2020-04-07', '20000')
Here you are trying to insert string in columns with a datatype int... this should be:
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES (1001, 1001, '2020-04-07', '20000')
Good luck!

MIN/MAX VALUES GROUP BY ID

Find the names and addresses of the customers with the highest balance and the people with the lowest loan in each branch.
Tables:
CREATE TABLE Branch(
BranchID INT NOT NULL,
Address varchar(100),
ManagerSsn varchar(100),
Branch_name varchar(255),
PRIMARY KEY (BranchID)
);
CREATE TABLE Has_Account(
Assn Int,
ANo CHAR(16),
Opened_date date,
PRIMARY KEY(Assn),
FOREIGN KEY(Assn) REFERENCES Customer(Ssn),
FOREIGN KEY(ANo) REFERENCES Account(AccountNo)
);
CREATE TABLE Account(
AccountNo CHAR(16) NOT NULL,
BranchID Int,
Balance Int,
PRIMARY KEY(AccountNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Customer(
Ssn Int CHECK(0101000000000 < Ssn < 3112999995999) NOT NULL,
Address VARCHAR(100),
Name VARCHAR(100),
EmployerID Int,
PRIMARY KEY(Ssn)
);
CREATE TABLE Loan(
LoanNo CHAR(16) NOT NULL,
BranchID Int,
Amount Int,
PRIMARY KEY(LoanNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Has_Loan(
LNo CHAR(16),
Lssn Int,
Repay_date date,
PRIMARY KEY (LNo),
FOREIGN KEY(Lno) REFERENCES Loan(LoanNo),
FOREIGN KEY(Lssn) REFERENCES Customer(Ssn)
);
Populating the tables
INSERT INTO CUSTOMER
VALUES('2004980890999', ‘Kanaribakken 2B’, ‘Paul Stolten’, ‘’);
INSERT INTO CUSTOMER
VALUES(‘1010968250234’, ‘Parkveien 5’, ‘Anders Nilsson’, ‘’, );
INSERT INTO CUSTOMER
VALUES(‘2104996495987’, ‘Kongeveien 24B’, ‘Karoline Torskerud’, ‘123’, );
INSERT INTO CUSTOMER
VALUES(‘2910943135444’, ‘Gåsesmauet 3’, ‘Bente Arildsen’, ‘555’, );
INSERT INTO CUSTOMER
VALUES(‘1304980890678’, ‘Knallebakken 30’, ‘Knut Solberg’, ‘555’,);
INSERT INTO CUSTOMER
VALUES('1019730000234', ‘Laksebakken 40’, ‘Harald Olsen’, ‘’);
INSERT INTO BRANCH
VALUES (‘123’, ‘Børsveien 49’, ‘1412963670786’, ‘Nesttun’,);
INSERT INTO BRANCH
VALUES(‘555’, ‘Vimpelveien 24’, ‘2412983905999’, ‘DNB’,);
INSERT INTO ACCOUNT
VALUES (‘1234567890087620’, ‘123’, ‘459’,);
INSERT INTO ACCOUNT
VALUES (‘8402849284910340’, ‘123’, ‘35120’,);
INSERT INTO ACCOUNT
VALUES (‘3452948293817280’, ‘555’, ‘69’,);
INSERT INTO HAS_ACCOUNT
VALUES(‘1010968250234’, ‘1234567890087620’, ‘17.04.2012’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2104996495987’, ‘8402849284910340’, ‘20.09.2000’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2910943135444’, ‘3452948293817280’, ‘29.01.2006’,);
INSERT INTO LOAN
VALUES (‘2291928394039280’, ‘555’, ‘50000’,);
INSERT INTO LOAN
VALUES (‘1295829384932100’, ‘123’, ‘400000’,);
INSERT INTO LOAN
VALUES ('1295829384932222', ‘123’, ‘40000’);
INSERT INTO HAS_LOAN
VALUES (‘2910943135444’, ‘2291928394039280’, ‘09.09.2019’,);
INSERT INTO HAS_LOAN
VALUES (‘2104996495987’, ‘1295829384932100’, ‘14.03.2022’,);
INSERT INTO HAS_LOAN
VALUES ('2004980890999', '1295829384932222', '01.02.2021');
Draft so far:
SELECT DISTINCT Customer.Name, Customer.Address
FROM Customer, Account, Loan, Has_Account, Has_Loan
WHERE Customer.Ssn = Has_Account.Assn AND Has_Account.Assn = Has_Loan.Lssn
(SELECT Account.Balance MAX(Balance) GROUP BY(BranchID)) AND
Loan.AMOUNT(SELECT Loan.Amount MIN(Amount) GROUP BY(BranchID));
I think it is correct that we need to group our output by BranchID, and max/min is ok. Looks like the statement formalities are wrong somehow.
Any help or hint is much appreciated!

difficulty inserting values in database

I have created my tables as shown below (I am using Oracle XE). The problem that occurs is that when I try to insert values into the tables, I get an error because the value of one's foreign key, has not been entered as a primary key of the other. For example, see tables DEPARTMENT and EMPLOYEE. Below, along with table creation code, is also the data inserting part. How to bypass that problem?
CREATE TABLE DEPARTMENT
(
DNAME VARCHAR(10),
DEPTID INTEGER NOT NULL,
MNG_IDNO INTEGER NOT NULL,
PRIMARY KEY (DEPTID)
);
CREATE TABLE EMPLOYEE
(
IDNO INTEGER NOT NULL,
FNAME VARCHAR(10) NOT NULL,
LNAME VARCHAR(10) NOT NULL,
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20),
AREA VARCHAR(20),
STREET VARCHAR(50),
HOUSENO VARCHAR(4),
ZIPCODE INTEGER,
BIRTHDAY DATE,
SALARY INTEGER,
IBAN VARCHAR(34),
DEPT_ID INTEGER NOT NULL,
PRIMARY KEY (IDNO)
);
CREATE TABLE CUSTOMER
(
IDNO INTEGER NOT NULL,
FNAME VARCHAR(10) NOT NULL,
LNAME VARCHAR(10) NOT NULL,
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20),
AREA VARCHAR(20),
STREET VARCHAR(50),
HOUSENO VARCHAR(4),
ZIPCODE INTEGER,
BIRTHDAY DATE,
PRIMARY KEY (IDNO)
);
CREATE TABLE CAR
(
VIN INTEGER NOT NULL,
BRAND VARCHAR(10) NOT NULL,
MODEL VARCHAR(10) NOT NULL,
COLOR VARCHAR(10) NOT NULL,
TRANTYPE VARCHAR(10) NOT NULL,
FUELTYPE VARCHAR(10) NOT NULL,
ENGCC INTEGER NOT NULL,
ENTRYDATE DATE,
PRICE INTEGER NOT NULL,
PAYMENTMETHOD VARCHAR(10) NOT NULL,
DEPT_ID INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PRIMARY KEY (VIN)
);
CREATE TABLE CAR_PARTS
(
PARTNUM VARCHAR(25) NOT NULL,
PARTNAME VARCHAR(25) NOT NULL,
COST INTEGER NOT NULL,
PRIMARY KEY (PARTNUM)
);
CREATE TABLE USED_CAR
(
CAR_VIN INTEGER NOT NULL,
MILEAGE INTEGER NOT NULL,
REGDATE DATE NOT NULL,
PRIMARY KEY (CAR_VIN)
);
CREATE TABLE SERVICE
(
DUE_DATE DATE,
ARR_DATE DATE,
COST INTEGER NOT NULL,
SERVICE_NUM INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PAYMENTMETHOD VARCHAR(10) NOT NULL,
CAR_VIN INTEGER NOT NULL,
PRIMARY KEY (SERVICE_NUM)
);
CREATE TABLE CUST_PHONUMBER
(
PHONE_NUM INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PHONE_TYPE VARCHAR(10),
PRIMARY KEY (CUST_IDNO, PHONE_NUM)
);
CREATE TABLE CAR_DEFECTS
(
DEFECTS VARCHAR(100) NOT NULL,
CAR_VIN VARCHAR(100) NOT NULL,
PRIMARY KEY (DEFECTS, CAR_VIN)
);
CREATE TABLE EMPLOYEE_PHONUMBER
(
PHONENUM INTEGER NOT NULL,
EMPL_IDNO INTEGER NOT NULL,
PRIMARY KEY (PHONENUM, EMPL_IDNO)
);
CREATE TABLE SERVICE_DETAILS
(
SERVICENUM INTEGER NOT NULL,
PROBLEMS VARCHAR(100) NOT NULL,
SOLUTION VARCHAR(100),
PRIMARY KEY (PROBLEMS, SERVICENUM)
);
CREATE TABLE HAS
(
PART_NUM VARCHAR(25) NOT NULL,
CAR_VIN INTEGER NOT NULL,
PRIMARY KEY (PART_NUM, CAR_VIN)
);
CREATE TABLE USES
(
PART_NUM VARCHAR(25) NOT NULL,
SERVICE_NUM INTEGER NOT NULL,
PRIMARY KEY (PART_NUM, SERVICE_NUM)
);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY (MNG_IDNO) REFERENCES EMPLOYEE(IDNO);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPTID);
ALTER TABLE CAR ADD FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPTID);
ALTER TABLE CAR ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE SERVICE ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE SERVICE ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE USED_CAR ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE HAS ADD FOREIGN KEY (PART_NUM) REFERENCES CAR_PARTS(PARTNUM);
ALTER TABLE HAS ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE USES ADD FOREIGN KEY (PART_NUM) REFERENCES CAR_PARTS(PARTNUM);
ALTER TABLE USES ADD FOREIGN KEY (SERVICE_NUM) REFERENCES SERVICE(SERVICE_NUM);
ALTER TABLE CUSTOMER_PHONUMBER ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE EMPLOYEE_PHONUMBER ADD FOREIGN KEY (EMPL_IDNO) REFERENCES EMPLOYEE(IDNO);
ALTER TABLE SERVICE_DETAILS ADD FOREIGN KEY (SERVICENUM) REFERENCES SERVICE(SERVICE_NUM);
Data Insert:
INSERT INTO EMPLOYEE VALUES (1234,'ANTREAS','GEORGIOU','MALE','NICOSIA','STROVOLOS','PANAYIAS','12',2682,TO_DATE('01.01.1989', 'DD.MM.YYYY'),5000,9999999,1);
INSERT INTO EMPLOYEE VALUES (1235,'KOSTAS', 'KOSTA', 'MALE', 'NICOSIA','STROVOLOS','VIZANTIOU','12',2064,TO_DATE('01.02.1980', 'DD.MM.YYYY'),5000,9999998,2);
INSERT INTO EMPLOYEE VALUES (1236,'MARIA', 'ANTREOU', 'FEMALE', 'NICOSIA','EGKOMI','TSAROU','12',2522,TO_DATE('05.05.1988', 'DD.MM.YYYY'),5000,9999995,3);
INSERT INTO EMPLOYEE VALUES (1237,'GEORGIA', 'ANTREOU', 'FEMALE', 'NICOSIA','EGKOMI','TSAROU','13',2522,TO_DATE('05.05.1978', 'DD.MM.YYYY'),5000,9999996,4);
INSERT INTO DEPARTMENT VALUES ('SERVICE',1,1234);
INSERT INTO DEPARTMENT VALUES ('SALES',2,1235);
INSERT INTO DEPARTMENT VALUES ('ACCOUNTING',3,1236);
INSERT INTO DEPARTMENT VALUES ('MANAGEMENT',4,1237);
INSERT INTO CUSTOMER VALUES (4321, 'ANTREAS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1954','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4322, 'MARIA', 'ANTREOU','FEMALE','PAFOS','GEROSKIPOU','VIZANTIOU','7',2525,TO_DATE('03.03.1953','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4323, 'KOSTAS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1970','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4324, 'ELENA', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2252,TO_DATE('04.04.1985','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4325, 'MARIOS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1987','DD.MM.YYYY'));
INSERT INTO CAR VALUES (1111111111, 'MAZDA', '3', 'BLUE', 'MANUAL', 'PETROL', 1600, TO_DATE('08.01.2013','DD.MM.YYYY'), 5000, 'CASH', 2, 4321);
INSERT INTO CAR VALUES (2222222222, 'TOYOTA', 'COROLLA','BLACK', 'MANUAL','PETROL',1600,TO_DATE('08.01.2013','DD.MM.YYYY'),7000,'FINANCE',2,4322);
INSERT INTO CAR VALUES (3333333333, 'TOYOTA', 'HILUX', 'GRAY', '4X4', 'DIESEL', 2500, TO_DATE('08.01.2013','DD.MM.YYYY'), 10000, 'CASH', 2 , 4325);
INSERT INTO CAR VALUES (4444444444, 'HONDA', 'CIVIC', 'BLACK', 'MANUAL', 'PETROL', 1300, TO_DATE('08.01.2013','DD.MM.YYYY'), 3000,'CASH', 2, 4323);
INSERT INTO CAR VALUES (5555555555, 'HONDA', 'INSIGHT', 'WHITE', 'AUTO', 'PETROL', 1500, TO_DATE('08.01.2013','DD.MM.YYYY'), 20000, 'FINANCE', 2, 4324);
INSERT INTO CAR_PARTS VALUES ('A20005B', 'RIGHT FRONT AXLE', 200);
INSERT INTO CAR_PARTS VALUES ('C15220C', 'AIR FILTER', 50);
INSERT INTO CAR_PARTS VALUES ('V99T', 'ENGINE GASKET', 80);
INSERT INTO CAR_PARTS VALUES ('A20004B', 'LEFT FRONT AXLE', 200);
INSERT INTO CAR_PARTS VALUES ('U9P002', 'INTAKE MANIFOLD', 300);
INSERT INTO USED_CAR VALUES (4444444444, 25000, TO_DATE('02.02.2000','DD.MM.YYYY'));
INSERT INTO USED_CAR VALUES (5555555555, 122000, TO_DATE('05.08.2003','DD.MM.YYYY'));
INSERT INTO SERVICE VALUES (TO_DATE('12.01.2013','DD.MM.YYYY'), TO_DATE('01.01.2013','DD.MM.YYYY'), 300, 0001, 4321, 'CASH', 1111111111);
INSERT INTO CUST_PHONUMBER VALUES (99123456, 4321, 'MOBILE');
INSERT INTO CUST_PHONUMBER VALUES (99849563, 4322, 'MOBILE');
INSERT INTO CUST_PHONUMBER VALUES (22568988, 4323, 'LANDLINE');
INSERT INTO CUST_PHONUMBER VALUES (25698974, 4324, 'LANDLINE');
INSERT INTO CUST_PHONUMBER VALUES (97584692, 4325, 'MOBILE');
INSERT INTO CAR_DEFECTS VALUES ('BROKEN SIDE MIRRORS|OIL LEAKS', 4444444444);
INSERT INTO CAR_DEFECTS VALUES ('RUSTY PAINT|WORN-OUT CLUTCH', 5555555555);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97989565, 1234);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97585858, 1235);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (96525263, 1236);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97484852, 1237);
INSERT INTO SERVICE_DETAILS VALUES (0001, 'BROKEN FRONT RIGHT AXLE', 'REPLACED FRONT RIGHT AXLE');
INSERT INTO HAS VALUES ('A20005B', 1111111111);
INSERT INTO HAS VALUES ('A20004B', 1111111111);
INSERT INTO USES VALUES ('A20005B', 0001);