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

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])

Related

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)
);

Movie Ticket System (DB)

I'm making a online movie reservation system on MVC and I am stuck at the database part on how the tables would link to each other. Does it seem alright?
ERD
CREATE TABLE [dbo].[Member]
(
[memberID] INT IDENTITY (1, 1) NOT NULL,
[memFirstName] VARCHAR (50) NULL,
[memLastName] VARCHAR (50) NULL,
[memEmailAddress] VARCHAR (50) NULL,
[memPassword] VARCHAR (15) NULL,
[memTelephone] CHAR (10) NULL,
[memAddress] VARCHAR (70) NULL,
PRIMARY KEY CLUSTERED ([memberID] ASC)
);
CREATE TABLE [dbo].[Movie]
(
[movieID] INT IDENTITY (1, 1) NOT NULL,
[movieName] VARCHAR (100) NULL,
[movieCategory] CHAR (10) NULL,
PRIMARY KEY CLUSTERED ([movieID] ASC)
);
CREATE TABLE [dbo].[Reservation]
(
[resID] INT IDENTITY (1, 1) NOT NULL,
[memberID] INT NOT NULL,
[showID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([resID] ASC),
FOREIGN KEY ([memberID]) REFERENCES [dbo].[Member] ([memberID]),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Show] ([showID])
);
CREATE TABLE [dbo].[Show]
(
[showID] INT IDENTITY (1, 1) NOT NULL,
[showDateTime] DATETIME NOT NULL,
[movieID] INT NULL,
CONSTRAINT [PK_Show] PRIMARY KEY CLUSTERED ([showID] ASC),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Movie] ([movieID])
);

Azure SQL Foreign Key with Visual Studio 2015

I have an Azure SQL database created in the management station, I connect to it on Visual Studio 2015, I create tables Player and Team, I'm trying to create a foreign key to reference team name but I keep getting the following error;
SQL71516 :: The referenced table '[dbo].[Team]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
From looking at online sources, mainly MSDN, I have tried a few ways to solve this issue but have had no luck. Here is my SQL code;
Team Table:
CREATE TABLE [dbo].[Team] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Wins] INT NOT NULL,
[Draws] INT NOT NULL,
[Losses] INT NOT NULL,
[GoalsFor] INT NOT NULL,
[GoalsAgainst] INT NOT NULL,
[GoalDifference] INT NOT NULL,
[Points] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Player Table:
CREATE TABLE [dbo].[Player] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Team] VARCHAR (30) NOT NULL,
[Goals] INT NOT NULL,
[Assists] INT NOT NULL,
[Apps] INT NOT NULL,
[Club] VARCHAR (30) NOT NULL,
CONSTRAINT [FK_Player_Club] FOREIGN KEY ([Club]) REFERENCES [dbo].[Team]([Name]),
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Team Table:
CREATE TABLE [dbo].[Team] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Wins] INT NOT NULL,
[Draws] INT NOT NULL,
[Losses] INT NOT NULL,
[GoalsFor] INT NOT NULL,
[GoalsAgainst] INT NOT NULL,
[GoalDifference] INT NOT NULL,
[Points] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Player Table:
CREATE TABLE [dbo].[Player] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Team] VARCHAR (30) NOT NULL,
[Goals] INT NOT NULL,
[Assists] INT NOT NULL,
[Apps] INT NOT NULL,
[TeamId] INT NOT NULL,
CONSTRAINT [FK_Player_Club] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Team]([Id]),
PRIMARY KEY CLUSTERED ([Id] ASC)
);

PostgreSQL constraint problems

I am having these two errors :
ERROR: there is no unique constraint matching given keys for referenced table "flight"
*** Error ***
ERROR: there is no unique constraint matching given keys for referenced table "flight"
This is my code :
CREATE TABLE Staff (
EmployeeNumber int NOT NULL,
FirstName char(15) NOT NULL,
LastName char(15) NOT NULL,
SocialSecurity int NOT NULL,
Sex char(1) NOT NULL,
Address char(20) NOT NULL,
City char(20) NOT NULL,
Province char(15) NOT NULL,
Country char(20) NOT NULL,
Primary Key (EmployeeNumber)
);
CREATE TABLE FlightAttendent (
FALN int,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
Foreign Key (EmployeeNumber) References Staff(EmployeeNumber),
Primary Key (FALN)
);
Create TABLE AircraftType (
ACType char (10),
Instrument char(1) NOT NULL,
Engines int NOT NULL,
CrewCount int NOT NULL,
PassengerCount int NOT NULL,
Primary Key (ACType)
);
CREATE TABLE Pilot (
PILN int,
MedicalValid date NOT NULL,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
AircraftType char (10) NOT NULL,
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber),
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (PILN)
);
Create TABLE Aircraft (
AircraftID char(6) NOT NULL,
AircraftManufacturer char(10) NOT NULL,
AircraftType char(10) NOT NULL,
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (AircraftID)
);
CREATE Table Airport (
AirportCode char(4) NOT NULL,
AirportName char(40) NOT NULL,
City char(20) NOT NULL,
Country char(20) NOT NULL,
Continent char(20) NOT NULL,
Primary Key (AirportCode)
);
Create TABLE Flight (
FlightID char (20),
FlightDate date,
AircraftID char(6) NOT NULL,
ArrivalAirport char(4) NOT NULL,
DepartureAirport char(4) NOT NULL,
PRIMARY KEY (FlightID, FlightDate),
FOREIGN Key (ArrivalAirport) references Airport(AirportCode),
FOREIGN Key (DepartureAirport) references Airport(AirportCode),
FOREIGN KEY (AircraftID) references Aircraft(AircraftID)
);
Create TABLE FlightCrew (
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
FlightDate date REFERENCES Flight(FlightDate) ON DELETE CASCADE,
EmployeeNumber int NOT NULL,
StaffRole char(20) NOT NULL,
PRIMARY KEY(FlightID, FlightDate),
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber)
);
CREATE Table Passenger (
PassengerNumber int,
PassportNumber int NOT NULL,
Citizenship char (20) NOT NULL,
FirstName char (20) NOT NULL,
LastName char (20) NOT NULL,
Primary Key (PassengerNumber)
);
CREATE Table PassengerManifest (
FlightID char(20),
FlightDate date,
PassengerNumber int NOT NULL,
Foreign Key (FlightDate) References Flight(FlightDate),
Foreign Key (PassengerNumber) References Passenger(PassengerNumber),
Primary Key (FlightID, FlightDate)
);
What did I do wrong? Thanks!
When you have multiple values in a primary key, you need to reference it differently as a foreign key.
Basically, when you say
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
PostgreSQL checks for that primary key, which doesn't exist (since the primary key on that table is (flightid, flightdate)).
So drop the REFERENCES clauses when referencing the flight table, and add
FOREIGN KEY (FlightID, FlightDate) REFERENCES Flight (FlightID, FlightDate)
In the manner you have in some of the other table definitions.