foreign key not adding to booking table, can anyone spot the issue - sql

Create table Hotel (
Hotel_no varchar (10) NOT NULL,
Hotel_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Hotel_no) );
Create table Room (
Room_no varchar (10) NOT NULL,
Hotel_no varchar (10) NOT NULL,
type varchar (40) NOT NULL,
price varchar (20) NOT NULL,
Primary key (Room_no, Hotel_no) );
Create table Guest (
Guest_no varchar (10) NOT NULL,
Guest_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Guest_no) );
Create table Booking (
date_from date,
date_to date,
Hotel_no varchar (10) NOT NULL,
Guest_no varchar (10) NOT NULL,
Room_no varchar (10) NOT NULL,
Primary key (date_from, Hotel_no, Guest_no),
Foreign key(Room_no)references Room(Room_no));
when i try adding the foreign key to the booking table it gives an ORA-02270 error, and i cant seem to figure out the problem
any assistance would be greatly appreciated.

Your foreign key on Room should reference the primary key Room(Room_no, Hotel_no)

If the primary key is a set of columns then the foreign key should also be a set of columns corresponding to the composite key columns.
Try below code :
Create table Booking ( date_from date, date_to date, Hotel_no varchar (10) NOT NULL, Guest_no varchar (10) NOT NULL, Room_no varchar (10) NOT NULL, Primary key (date_from, Hotel_no, Guest_no), Foreign key(Room_no,Hotel_no)references Room(Room_no,Hotel_no);

Related

How can i solve the unique constraint error in sql program?

I have homework to create tables and insert data into it and when i insert data into Branch table it gives me a unique constraint error and I don’t know why, and how can I solve this problem. my codes:
create table Branch(
Branchno varchar (4) primary key not null,
Street varchar (15) not null,
City varchar (10) not null,
Postcode varchar (10) not null)
create table staff (
Staffno varchar (4),
Fname varchar2 (50) not null,
Lname varchar (50),
Position varchar2 (50),
Sex varchar (1),
Dob date,
Salary number (30),
Branchno varchar (4),
constraint pk_staff primary key (staffno),
constraint fk_staff_branchno foreign key (branchno)
references Branch (branchno)
)
create table Client
(Clientno varchar (4) primary key not null,
Fname varchar (50) not null,
Lname varchar (50) not null,
Telno varchar (50) not null,
Preftype varchar (18) not null,
Maxrent INT)
create table PropertyForRent(
Propertyno varchar (40) not null,
Street varchar (40),
City varchar (40),
Postcode varchar (40),
Type varchar (40),
Rooms number (30),
Rent number (30),
Ownerno varchar (40),
Staffno varchar (40),
Branchno varchar (40),
constraint pk_PropertyForRent primary key (Propertyno),
foreign key (staffno)
references staff (staffno),
foreign key (branchno)
references branch (branchno)
)
create table Registration
(clientno varchar (60) not null,
branchno varchar (50) not null,
staffno varchar (50) not null,
datejoined date not null,
foreign key (clientno) references client (clientno),
foreign key (branchno) references branch (branchno),
foreign key (staffno) references staff (staffno)
)
The data that I insert to give me this error:
INSERT INTO Branch values
('B005','22 Deer Rd','London','SW1 4EH');
INSERT INTO Branch values
('B007','16 Argy11 St','Aberdeen','AB2 3SU');
I try with Oracle 11g and not receive any error:
SQL> INSERT INTO Branch values
('B005','22 Deer Rd','London','SW1 4EH'); 2
1 row created.
SQL> INSERT INTO Branch values
('B007','16 Argy11 St','Aberdeen','AB2 3SU'); 2
1 row created.
SQL> commit;
Commit complete.
Probably have you already record with these keys or have you any trigger on this table.
Instead of an INSERT INTO
you could
MERGE INTO Branch dest
USING( SELECT 'B005' Branchno,'22 Deer Rd' Street,'London' City,'SW1 4EH' Postcode FROM dual) src
ON( dest.Branchno = src.Branchno )
WHEN NOT MATCHED THEN
INSERT( Branchno, Street, City,Postcode )
VALUES ( src.Branchno, src.Street, src.City,src.Postcode );
That looks on the first sight, more difficult, but you would not get an error and all stops.
trggers can't stop you from inserting doulbe entires as they you can only can raise errors with wuld stop again every multi insert
see example

How can I add two different features as a foreign key to my SQL Column?

I have a Car table in my Sql Data and every car has own BrandId that declared every Brands in "Brands" table as a foreign key. I'm just wondering what if I want to add two different BrandId together in my one Car>BrandId Column, what would I do? How can it be possible for adding two features as a foreign key in one spesific column?
Here is my Car Tables Code
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId]) REFERENCES [dbo].[Brands] ([BrandId])
);
And here te code of my Brands table;
CREATE TABLE [dbo].[Brands] (
[BrandId] INT IDENTITY (1, 1) NOT NULL,
[BrandName] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([BrandId] ASC)
);
You can simply create your table as follows :
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId1] INT NULL,
[BrandId2] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId1]) REFERENCES [dbo].[Brands] ([BrandId]),
FOREIGN KEY ([BrandId2] REFERENCES [dbo].[Brands] ([BrandId])

doctor schedulering design SQL server

Im trying to run this line of code to create a database to schedule doctor.
EmployeeID int primary key not null,
DPhone int not null,
DFirstName nvarchar (20) not null,
DLastName nvarchar (25) not null,
Specialization nvarchar (100) not null,
Cost int not null);
create table schedule(
DEmployeeID int not null, foreign key (DEmployeeID) references doctor (EmployeeID),
SDay nvarchar (9) not null,
SDate date not null,
STime time not null,
BookingStatus char not null,
primary key (DEmployeeID, SDate, STime),
PMedicalID int, foreign key (PMedicalID) references patient (MedicalID));
create table medRecords (
prescription nvarchar (100),
diagnosis nvarchar (100) not null,
Pdescription text not null,
medRecID int not null primary key,
MedicalID int, foreign key (MedicalID) references patient (MedicalID),
SDate date not null, foreign key (SDate) references schedule (SDate),
EmployeeID int not null, foreign key (EmployeeID) references doctor (EmployeeID))
This is the error
There are no primary or candidate keys in the referenced table 'Schedule' that match the referencing column list in the foreign key 'FK__MedRecord__SDate__3D5E1FD2'.
The Sdate is already a primary key so i don't understand why i get this error.
edit
I already have a patiens table created

How to reference only one attribute as a foreign key from a primary key composited of two attributes

CREATE TABLE Performances (
DT Datetime NOT NULL,
Name varchar(20) NOT NULL,
Start_Time time NULL,
Min_price integer NOT NULL,
Max_price integer NOT NULL,
Location Varchar (20) NOT NULL,
PRIMARY KEY (DT, Name),
create Table [TYPE OF SHOWS] (
Name Varchar (20) NOT NULL ,
Type_of_show Varchar (20),
PRIMARY KEY (Name),
CONSTRAINT fk_PerformanceName FOREIGN KEY (Name)
REFERENCES Performances (Name),
)
I'm unable to set the foreign key only to Name, how can I implemnt this?
I suspect that you actually want the relation the other way around, with Performances referencing Type of Shows, like:
create Table [TYPE OF SHOWS] (
Name Varchar (20) NOT NULL ,
Type_of_show Varchar (20),
PRIMARY KEY (Name)
);
CREATE TABLE Performances (
DT Datetime NOT NULL,
Name varchar(20) NOT NULL,
Start_Time time NULL,
Min_price integer NOT NULL,
Max_price integer NOT NULL,
Location Varchar (20) NOT NULL,
PRIMARY KEY (DT, Name),
CONSTRAINT fk_PerformanceName FOREIGN KEY (Name) REFERENCES [TYPE OF SHOWS](Name)
);

How to make (patient id) forgein key in table of bill?

I try to create three tables by using website suport compiler any code but I have a problem in the table of the bill.
When I run it I get to error show me it is near in foreign key
These are codes of three tables
CREATE TABLE patient (
Patient Id (5) Primary key,
Name Varchar (20) Not null ,
Age Int Not null ,
Weight Int Not null ,
Gender Varchar (10) Not null,
Address Varchar (50) Not null ,
Disease Varchar (20) Not null
);
CREATE TABLE doctors (
DoctorId Varchar (5) Primary key,
Doctorname Varchar (15) Not null,
Dept Varchar (15) Not null
);
CREATE TABLE bill (
Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5) Foreign key,,
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null
);
Patient Table
CREATE TABLE patient
(
patient_id VARCHAR (5) PRIMARY KEY,
name VARCHAR (20) NOT NULL,
age INT NOT NULL,
weight INT NOT NULL,
gender VARCHAR (10) NOT NULL,
address VARCHAR (50) NOT NULL,
disease VARCHAR (20) NOT NULL
);
Errors
No data type has been assigned in Patient id column (Patient Id (5)
Primary key)
Patient id column name contains spaces. You need to
enclose the column name in double quotes or replace space with something else
(ex: _). It's not recommended to use spaces.
A column name with space
CREATE TABLE tablename ("column name" datatype);
Doctors Table
CREATE TABLE doctors
(
doctorid VARCHAR (5) PRIMARY KEY,
doctorname VARCHAR (15) NOT NULL,
dept VARCHAR (15) NOT NULL
);
Bill Table
CREATE TABLE bill
(
bill_no VARCHAR (50) PRIMARY KEY,
patient_id VARCHAR (5),
doctor_charge INT NOT NULL,
patient_type VARCHAR (10) NULL,
no_of_days INT NULL,
lab_charge INT NULL,
bill INT NOT NULL,
FOREIGN KEY (patient_id) REFERENCES patient(patient_id)
);
Errors
The way you have assigned foreign key is wrong. Please refer this and this article for more information. (Patient_Id Varchar (5) Foreign key,,)
There are two commans in the Patient_Id column (Patient_Id Varchar (5) Foreign key,,)
You have to provide reference of the table for which you want to use the reference key.
For example, you have table Persons which has Primary key PersonID, in that case if you want to use that as foreign key in another table, lets say Orders.
In Oracle
CREATE TABLE Orders (
OrderID numeric(10) not null,
OrderNumber numeric(10) not null,
PersonID numeric(10) not null,
CONSTRAINT fk_person_id
FOREIGN KEY (PersonID )
REFERENCES Persons(PersonID )
Your Case :
CREATE TABLE bill
( Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5),
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null,
CONSTRAINT fk_patient_id
FOREIGN KEY (Patient_Id)
REFERENCES patient(Patient_Id)
);
Remove the 'Foreign Key' from the table creation script.
Add this to your SQL script:
ALTER TABLE [Bill] WITH CHECK ADD CONSTRAINT [FK_Bill_Patient] FOREIGN KEY([Patient_Id])
REFERENCES [Patient] ([Patient_Id])
GO
ALTER TABLE [Bill] CHECK CONSTRAINT [FK_Bill_Patient]
GO
The words FOREIGN KEY are only needed for introducing the name of the FK constraint. Since your other constraints are not named, you might as well skip that part and go straight to REFERENCES.
If you specify a foreign key constraint as part of the column definition, you can omit the datatype to allow it to inherit from its parent at the time of creation, which I think is good practice as the types will automatically match.
We use VARCHAR2 in Oracle, not VARCHAR.
You don't need to specify NULL for columns that are allowed to be null.
I am not sure a 5-character string is a good datatype for a unique ID. How will you generate the values? Normally an auto-incrementing sequence number simplifies this.
create table doctors
( doctorid varchar2(5) primary key
, doctorname varchar2(15) not null
, dept varchar2(15) not null );
create table patients
( patient_id varchar2(5) primary key
, name varchar2(20) not null
, age integer not null
, weight integer not null
, gender varchar2(10) not null
, address varchar2(50) not null
, disease varchar2(20) not null );
create table bills
( bill_no varchar2(50) primary key
, patient_id references patients -- Allow datatype to inherit from parent
, patient_type varchar2(10)
, no_of_days integer
, lab_charge integer
, bill integer not null );