For some odd reason I can't create a table and because if that I can't insert any values into those tables. This is a new database and I am having a bit of a brain fart.. any help??? thanks
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY (Agencies)
)
Example of a working code
create table Agencies
(
AgencyID nvarchar(20) primary key,
AgName nvarchar(40),
AgAddress nvarchar(40),
AgPhone int
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(20) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Hope it helps!
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(Your_Agencies_ID_COLUMN)
)
There two ways of creating foreign key constrain:
creating a foreign key constraint within the table definition
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
, CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID])
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
,CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID])
);
creating a foreign key constraint for existing table
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
,CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID] )
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
);
ALTER TABLE [dbo].[DataSourceComments]
ADD CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID]);
Note, that in both cases I am specifying the FK constraint name. It's is a recommended to have some naming convention which to apply to all new FK.
I believe your foreign key is incorrect... maybe try this?
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Supposing "AgencyID" is the ID in your Agencies Table.
Try the below code
CREATE TABLE Agency
(
AgencyID INT PRIMARY KEY IDENTITY,
Name NVARCHAR(20)
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID INT FOREIGN KEY REFERENCES Agency(AgencyID)
)
First you have to create the parent table ie, Agency table. Then create the child (Customers) and refer the parent.
Related
Foreign key 'id_client' references invalid column 'id_client' in
referencing table 'nrcomanda'.
use Logistica
create table client
(
id_client int primary key identity(1,1),
nume varchar(20) not null,
prenume varchar(20) not null,
id_nrc int foreign key references nrcomanda(id_nrc)
)
create table categorie
(
id_categorie int primary key identity(1,1),
categorie varchar(50),
)
create table pachet
(
id_pachet int primary key identity(1,1),
tip_pachet varchar(50)
)
create table transport
(
id_transport int primary key identity(1,1),
tip_transport varchar(20)
)
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)
create table raion
(
id_raion int primary key identity(1,1),
nume varchar(50)
)
create table localitate
(
id_loc int primary key identity(1,1),
id_raion int,
nume varchar(50)
foreign key(id_raion) references raion(id_raion)
)
create table adresa
(
id_adresa int primary key identity(1,1),
id_raion int foreign key references raion(id_raion),
id_loc int foreign key references localitate(id_loc),
id_nrc int foreign key references nrcomanda(id_nrc),
id_tara int foreign key references tara(id_tara),
strada varchar(30),
nr varchar(6),
ap varchar(6),
bloc varchar(6),
activ bit default 1
)
create table tara
(
id_tara int primary key identity(1,1),
nume varchar(20)
)
i think you have miss some column definitions in your create table :
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
Id_client int,
Id_categorie int,
...
Id_adresa int,
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)
create database shop1
use shop1
create table mathang
(
MatHangID INT primary key not null,
TenMatHang varchar(50),
SoLuong int not null,
Price int not null,
)
create table nhacungcap
(
MatHangID INT foreign key references mathang,
TenNhaCungCap varchar(50) ,
DiaChi varchar(100),
SoDienThoai int ,
CONSTRAINT pk_M_CC primary key (MatHangID)
)
create table khachhang
(
KhachHangID int not null primary key,
TenKhachHang varchar(50) not null,
[DiaChi] varchar(100) not null,
[SoDienThoai] varchar(50) not null,
)
create table donhang
(
DonHangID int references to khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
I can not find errors with this, it shows the foreign key error to add this relation ship. Any one please help.
Remove the to in
...
DonHangID int references to khachhang(KhachHangID)
...
to get
...
DonHangID int references khachhang(KhachHangID)
...
. The to doesn't belong there syntactically.
I am fairly new to SQL and I can't understand why I am receiving an error when establishing foreign keys as I receive an error saying that the destination table is invalid.
Below is the SQL code, any advice on how to fix would be brilliant! :)
The error appears regarding tblFilms and tblCinemaScreens.
CREATE TABLE tblCustomer (
CustomerID int,
CustomerSurname NVARCHAR(25),
CustomerForename NVARCHAR(20),
CustomerAge int,
CustomerPhoneNumber NVARCHAR(12),
CustomerEmailAddress NVARCHAR(100),
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
FilmShowings TIME,
PriceOfFilm MONEY,
DateOfBooking DATE,
FilmID int,
CinemaScreenID int,
CustomerID int,
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
CinemaScreenType NVARCHAR(10),
NumberOfSeats int,
FilmID int,
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID),
CONSTRAINT FK_tblCinemaScreens FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
)
GO
Cinema screens table does not need a film id and you need to create the tables BEFORE referencing them with FK's
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
CinemaScreenType NVARCHAR(10),
NumberOfSeats int,
FilmID int,
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblCustomer (
CustomerID int,
CustomerSurname NVARCHAR(25),
CustomerForename NVARCHAR(20),
CustomerAge int,
CustomerPhoneNumber NVARCHAR(12),
CustomerEmailAddress NVARCHAR(100),
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
You are trying to create a foreign key on tables before they are made. Comment out the lines in the create table statements that are giving you an error and create the tables. Once the tables are made create the missing foreign keys you need like this:
alter table tblFilms
add CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
alter table tblBookings
add CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
The answer is they should have not direct relationships
A film is shown on 0 or more screens
You need a join table
rblFilmCinema
filmID fk to tblFilms
screenID fk to tblCinemaScreens
composite PK on filmID, screenID
I would change up the schema some to give yourself more flexibility. I would assume that a film can be on more than one cinema screen so I would take CinemaScreenID off of tblFilms and remove the foreign key. You can also remove FilmID from tblBookings since you have the CinemaScreenID already which has the FilmID.. Another thing to consider might be having multiple films on the same CinemaScreen which would be another tabled called tblCinemaScreenFilms and you would put that CinemaScreenFilmID on the tblBookings instead
CREATE TABLE tblFilms (
FilmID int,
...
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
)
GO
CREATE TABLE tblCustomer (
CustomerID int,
...
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
CinemaScreenID int,
CustomerID int,
...
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_CinemaScreenID FOREIGN KEY (CinemaScreenID ) REFERENCES tblCinemaScreens(CinemaScreenID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID)
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
FilmID int,
...
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID),
CONSTRAINT FK_tblCinemaScreens FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
)
GO
Option B (preferred)
CREATE TABLE tblCustomer (
CustomerID int,
...
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
...
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
...
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID)
)
GO
CREATE TABLE tblCinemaScreenFilms (
CinemaScreenFilmID int,
CinemaScreenID int,
FilmID int
CONSTRAINT PK_tblCinemaScreenFilms PRIMARY KEY CLUSTERED (CinemaScreenFilmID),
CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID),
CONSTRAINT FK_CinemaScreenID FOREIGN KEY (FilmID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
CustomerID int,
CinemaScreenFilmID int,
...
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID),
CONSTRAINT FK_CinemaScreenFilmID FOREIGN KEY (CinemaScreenFilmID) REFERENCES tblCinemaScreenFilms(CinemaScreenFilmID)
)
GO
You're creating a table - tblFilms - and adding a foreign key to tblCinemaScreens before the second table has been created.
As a help I usually create all tables and then create any foreign key relationships and other constraints.
I am getting an error when i create my tabels.
The problem is that AssCode is not unique, so i can set it to unique, the combination of courseCode and AssCode is unique, thats why they are set as primary keys. I am using postgressql
here is the error:
ERROR: there is no unique constraint matching given keys for referenced table "assignments"
SQL state: 42830
here is my code:
CREATE TABLE Teachers (
BSN int primary key,
Surname varchar(40) NOT NULL,
Name varchar(40) NOT NULL
);
CREATE TABLE Courses (
CourseCode varchar(10) primary key,
Name varchar(20) NOT NULL
);
CREATE TABLE Assignments (
CourseCode varchar(10) REFERENCES Courses ON DELETE CASCADE,
AssCode varchar(10),
primary key(CourseCode,AssCode),
DependOn varchar(10),
Year date,
week int
);
CREATE TABLE WorkOn (
BSN int REFERENCES Teachers(BSN),
CourseCode varchar(10) REFERENCES Assignments(CourseCode),
AssCode varchar(10) REFERENCES Assignments(AssCode),
primary key (CourseCode,BSN,AssCode)
);
I found the answer:
CREATE TABLE WorkOn (
BSN int primary key REFERENCES Teachers(BSN),
CourseCode varchar(10),
AssCode varchar(10),
foreign key (CourseCode, AssCode) references Assignments (CourseCode, AssCode)
);
Why is the table Room not accepting the foreign key?
CREATE TABLE RoomType (
Roomtype nvarchar(2) NOT NULL,
Description nvarchar(20),
Responsibility nvarchar(20),
primary key (Roomtype)
)
Create table Room (
RoomID nvarchar(2) NOT NULL,
Capacity numeric(3)
)
ALTER TABLE Room
add foreign key(Roomtype)
references RoomType(Roomtype)
This is the error message I get when I run alter table.
Major Error 0x80040E11, Minor Error 0
ALTER TABLE Room
add foreign key(Roomtype)
references RoomType(Roomtype)
Invalid column ID. [ Roomtype ]
You need to add the foreign key as a field to the Room table before you attempt to declare the foreign key constraint.
CREATE TABLE RoomType (
Roomtype nvarchar(2) NOT NULL,
Description nvarchar(20),
Responsibility nvarchar(20),
primary key (Roomtype)
)
Create table Room (
RoomID nvarchar(2) NOT NULL,
Capacity numeric(3)
)
ALTER TABLE Room
ADD Roomtype nvarchar(2) NOT NULL
ALTER TABLE Room
add constraint FK_Give_Me_A_Good_Name foreign key(Roomtype)
references RoomType(Roomtype)
The column must exist BEFORE you can FK to it.
CREATE TABLE RoomType (
Roomtype nvarchar(2) NOT NULL,
Description nvarchar(20),
Responsibility nvarchar(20),
primary key (Roomtype)
)
Create table Room (
RoomID nvarchar(2) NOT NULL,
Capacity numeric(3),
RoomtypeA nvarchar(2) NOT NULL
)
ALTER TABLE [dbo].[Room] ADD CONSTRAINT FK_MyName FOREIGN KEY (RoomtypeA) REFERENCES dbo.Roomtype (Roomtype)
GO