SQL: How to connect a foreign key to primary key in the some table - sql

I have an ERD and I need to connect the foreign key to the primary key on the same table. I have tried the below SQL code:
CREATE TABLE Category(
CategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_cid_pk PRIMARY KEY DEFAULT NEWID(),
CategoryName VARCHAR(100)
CONSTRAINT cat_can_nn NOT NULL,
ParentCategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_pcid_fk REFERENCES SWD6_1B.[Category]([CategoryID]);
)
And below I have the ERD
Does anyone know how to do this?

I think you are trying to do something like:
CREATE TABLE Category(
CategoryID UNIQUEIDENTIFIER CONSTRAINT cat_cid_pk PRIMARY KEY DEFAULT NEWID(),
CategoryName VARCHAR(100) CONSTRAINT cat_can_nn NOT NULL,
ParentCategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_pcid_fk foreign key (ParentCategoryID) REFERENCES [Category]([CategoryID])
);

Create the table and then use alter table:
alter table Category add constraint cat_pcid_fk
foreign key (ParentCategoryID) references SWD6_1B.[Category]([CategoryID]);

Related

Add constraints for a foreign key that references multipleprimary keys from different tables in SQL Plus?

I'm a beginner learning SQL and am having trouble implementing this concept.
Suppose you create the following three tables:
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk1 REFERENCES dogOwner(ownerNo)
CONSTRAINT ownerNo_fk2 REFERENCES catOwner(ownerNo)
);
How do you properly create constraints for the foreign key ownerNo, which references ownerNo from two other tables?
You can't. You could have 2 columns in petsAdopted: dogOwnerNo and catOwnerNo and 2 foreign keys. But the table design doesn't seem to make sense: surely a pet either is a dog or a cat (or something else) regardless of who owns it?
Here is an alternative design:
CREATE TABLE owner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
petType VARCHAR2(10) NOT NULL CONSTRAINT petTypeChk (CHECK petType in ('CAT','DOG'))
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk REFERENCES owner(ownerNo)
);
This only address syntax part of your answer which You have got lot of syntax error, but this will not work, Please rethink your design.
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8),
ownerName VARCHAR(10),
CONSTRAINT ownerNo_pk2 PRIMARY KEY (ownerNo),
CONSTRAINT ownerNo_fk1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) ,
ownerNo VARCHAR(8),
CONSTRAINT petNo_pk PRIMARY KEY (petNo),
CONSTRAINT ownerNo_fk_pet1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo),
CONSTRAINT ownerNo_fk_pet2 FOREIGN KEY (ownerNo) REFERENCES catOwner(ownerNo)
);

Database constraint over multiple tables

Say I have the following tables:
CREATE TABLE [Weeks]
(
[Id] INT NOT NULL IDENTITY,
CONSTRAINT [PK_Weeks] PRIMARY KEY ([Id])
);
CREATE TABLE [Days]
(
[Id] INT NOT NULL IDENTITY,
[WeekId] INT NULL,
CONSTRAINT [PK_Days] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Days_Weeks_WeekId]
FOREIGN KEY ([WeekId]) REFERENCES [Weeks] ([Id])
);
CREATE TABLE [ReplacedDayInWeek]
(
[Id] INT NOT NULL IDENTITY,
[WeekId] INT NOT NULL,
[DayId] INT NOT NULL,
[ReplacedDayId] INT NOT NULL,
CONSTRAINT [PK_ReplacedDayInWeek] PRIMARY KEY ([Id]),
CONSTRAINT [FK_ReplacedDayInWeek_Days_DayId]
FOREIGN KEY ([DayId]) REFERENCES [Days] ([Id]),
CONSTRAINT [FK_ReplacedDayInWeek_Weeks_WeekId]
FOREIGN KEY ([WeekId]) REFERENCES [Weeks] ([Id]),
CONSTRAINT [FK_ReplacedDayInWeek_Weeks_ReplacedWeekId]
FOREIGN KEY ([ReplacedWeekId]) REFERENCES [Weeks] ([Id])
);
The table ReplacedDayInWeek contains a day in a specific week that's replaced by another day.
How can I create a SQL constraint (or perhaps another SQL based solution) that makes sure I can only insert rows into DayInWeek with a DayId that's part of the same week as WeekId?
I'm looking for a solution with the least amount of changes to the source database. I'd prefer an additional table or table changes above stored procedures.
This data structure makes no sense to me. You have the mapping between days and weeks in two tables.
You should really only be storing the week in one table and looking it up in the other.
That said, you can do what you want using an additional constraint on day/week between the two tables:
CREATE TABLE [Days]
(
[Id] INT NOT NULL IDENTITY,
[WeekId] INT NULL,
CONSTRAINT [PK_Days] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Days_Weeks_WeekId]
FOREIGN KEY ([WeekId]) REFERENCES [Weeks] ([Id]),
CONSTRAINT UNQ_Days_WeekId_Id UNIQUE (WeekId, Id)
);
CREATE TABLE [DaysInWeek]
(
[Id] INT NOT NULL IDENTITY,
[WeekId] INT NOT NULL,
[DayId] INT NOT NULL,
CONSTRAINT [PK_DaysInWeek] PRIMARY KEY([Id]),
CONSTRAINT [FK_DaysInWeek_Days_WeekId_DayId]
FOREIGN KEY (WeekId, DayId) REFERENCES Days(WeekId, Id),
CONSTRAINT [FK_DaysInWeek_Weeks_WeekId]
FOREIGN KEY ([WeekId]) REFERENCES [Weeks] ([Id])
);

Make attribute primary and foreign key in sql

How can I make an attribute a primary key within a table but also as a foreign key which references another table using sql in sql developer?
I know how to make it an attribute as a foreign key and a primary separate but not as a primary key as well as a foreign key
That's perfectly normal. For example:
create table employee (
id number(6) primary key not null,
name varchar2(50)
);
create table employee_desk (
desk_id number(6) primary key not null, -- PK and FK!
location varchar2(20),
constraint fk1 foreign key (desk_id) references employee (id)
);
The column desk_id is the primary key of the table employee_desk, and also a foreign key that points to the table employee.
Below is the example of primary key with foreign key
create table animals (id integer primary key);
create table cats (
id integer primary key
, name varchar(100) not null
, constraint d_cats_animals_fk foreign key (id) references animals (id)
);

Error There are no primary or candidate keys in the referenced table

I'm getting this error when trying to create a table with foreign key:
There are no primary or candidate keys in the referenced table 'TeamToPlayers' that match the referencing column list in the foreign key 'FKey2'.
I don't understand why, there is a primary key in the table TeamToPlayers.
Here are the queries:
create table TeamToPlayers
(TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES TeamToPlayers(PlayerName)
);
Table TeamToPlayers primary key consists of two fields - you must reference both as otherwise it's not a key. I think you may have your key the wrong way round - it should be on TeamToPlayers and referencing Players like so:
create table TeamToPlayers
(
TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES Players(PlayerName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
);

I am creating a database and i keep getting "INVALID IDENTIFIER" Error

CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
You should do it this way:
CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
constraint constraint_1_name FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
constraint constraint_2_name FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
(Of course instead of paperId/Revi you should use existing field names...)