Determine table structure - sql

I want to create db for anime. This is my ER structure:
https://ibb.co/ctRk8f6
Any tips to improve this?
Every anime has blob for image and relations to brand and episode, each episode may have multiple links and relation to tag.
I wanted to know if this structure is good or should I make separate table for image or any other tip
CREATE TABLE Brand
(
id INT NOT NULL,
name INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Tag
(
id INT NOT NULL,
name INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Anime
(
id INT NOT NULL,
name INT NOT NULL,
img INT NOT NULL,
id INT,
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES Brand(id)
);
CREATE TABLE Episode
(
order INT NOT NULL,
rating INT NOT NULL,
date INT NOT NULL,
id INT NOT NULL,
PRIMARY KEY (order, id),
FOREIGN KEY (id) REFERENCES Anime(id)
);
CREATE TABLE Link
(
link INT NOT NULL,
host INT NOT NULL,
order INT NOT NULL,
id INT NOT NULL,
PRIMARY KEY (link, order, id),
FOREIGN KEY (order, id) REFERENCES Episode(order, id)
);
CREATE TABLE TagOfAnime
(
id INT NOT NULL,
id INT NOT NULL,
PRIMARY KEY (id, id),
FOREIGN KEY (id) REFERENCES Anime(id),
FOREIGN KEY (id) REFERENCES Tag(id)
);

A few issues here:
Why is everything of type INT? I would assume that name would be a nvarchar of some maximum length nvarchar(200) perhaps?
Your TagOfAnime table has two columns named id. That will not work. If they are references to the primary keys to the tag and Anime tables, then you should name them as such: Tag_id and Anime_id
I would not name a table Link. What is it "linking"? I would make it more descriptive.
In general, add a table if there can be multiple items associated with a single row in another table - as you are doing with the tags for Animes. Otherwise, if the data can appear in additional columns of a single row of the base table, then consolidate your tables to avoid complexity.
Other than that, ask specific questions where you are stuck and I'm sure someone will jump in to help.

Related

Adding SQL constraint on attribtes of different tables

I have tables
Question (int id,string question, int questionNumber)
Option (int id, string option, int optionNumber)
How can I constrain pair (questionNumber, optionNumber) to be unique?
How can I apply this through Spring data JPA?
You have business keys; a question number and option numbers. A question number must uniquely identify a question. And an option number must uniquely identify an option within a question.
This would be something along the lines of:
CREATE TABLE question
(
question_number INT NOT NULL,
question VARCHAR(1000) NOT NULL,
PRIMARY KEY (question_number)
);
CREATE TABLE option
(
question_number INT NOT NULL,
option_number INT NOT NULL,
option VARCHAR(1000) NOT NULL,
PRIMARY KEY (question_number, option_number),
FOREIGN KEY (question_number) REFERENCES question(question_number)
);
You want to add technical IDs to your tables, though, and it seems you want to use them as primary keys. That makes this a little more complicated.
CREATE TABLE question
(
id INT NOT NULL,
question_number INT NOT NULL,
question VARCHAR(1000) NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT uq_question UNIQUE (question_number)
);
CREATE TABLE option
(
id INT NOT NULL,
question_id INT NOT NULL,
option_number INT NOT NULL,
option VARCHAR(1000) NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (question_id) REFERENCES question(id),
CONSTRAINT uq_option UNIQUE (question_id, option_number)
);
With a database built on technical primary keys you want your IDs to be unique and your business keys, too. So you end up with more unique constraints in the database.

References with PostgreSQL

I have this table:
CREATE TABLE cars_info.cars
(
id SERIAL,
owner_id INTEGER,
brand VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
color VARCHAR(50) NOT NULL,
register_number VARCHAR(50) NOT NULL,
created DATE NOT NULL,
PRIMARY KEY(id, brand, model, color, register_number, created),
CONSTRAINT fk_owner_id
FOREIGN KEY(owner_id)
REFERENCES persons_info.persons(id)
);
But when I tried create another table like this:
CREATE TABLE cars_info.violations
(
id SERIAL PRIMARY KEY,
car_id INTEGER NOT NULL,
message VARCHAR(100) NOT NULL,
active BOOLEAN NOT NULL,
CONSTRAINT fk_car_id
FOREIGN KEY(car_id)
REFERENCES cars_info.cars(id)
);
I get an error about that
Target external table "cars" does not have a unique constraint corresponding to the given keys
How can I fix that? I'm a beginner in SQL and don't know how to go about googling that
Your primary key definition for cars
PRIMARY KEY(id, brand, model, color, register_number, created)
makes no sense: The id column, being serial, is itself unique and it alone should be the primary key.
Delete your primary key definition and change the id column definition to:
id serial not null primary key
Unrelated, but best practice is to name table in the singular; name your tables car and violation rather than cars and violations

Adding an Array of INT column where each value is a primary key from another table

Given two tables like so
CREATE TABLE participants(
id SERIAL PRIMARY KEY,
Name TEXT NOT NULL,
Title TEXT NOT NULL
);
CREATE TABLE meetings (
id SERIAL PRIMARY KEY,
Organizer TEXT NOT NULL,
StartTime DATE NOT NULL,
EndTime DATE NOT NULL,
Participants INT[],
);
I want Participants column of 'meetings' table to contain set of integers which are all primary keys (specific participant) from 'participants' table.
How do I define this field in 'meetings' table ?
The old fashioned way is to create a many-many table, with a couple of commonsense constraints:
CREATE TABLE meetings_participants(
meeting_id int not null,
participant_id int not null,
primary key (meeting_id, participant_id),
foreign key(meeting_id) references meetings(id),
foreign key(participant_id) references participants(id)
)
Now it is easy to add and remove people to meetings be inserting or deleting rows or query meetings that e.g. have 4 or more participants.
A more common approach is to create a junction table for the meeting participants.
CREATE TABLE participants (
participant_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Name TEXT NOT NULL,
Title TEXT NOT NULL
);
CREATE TABLE meetings (
meeting_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Organizer TEXT NOT NULL,
StartTime DATE NOT NULL,
EndTime DATE NOT NULL
);
CREATE TABLE meeting_participants(
meeting_id INT NOT NULL,
participant_id INT NOT NULL,
PRIMARY KEY (meeting_id, participant_id),
FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id),
FOREIGN KEY (participant_id) REFERENCES participants(participant_id)
);
Which is then used to join the 2 tables.
For example:
SELECT m.*, p.*
FROM meeting_participants mp
JOIN meetings m USING(meeting_id)
JOIN participants p USING(participant_id)
WHERE m.Organizer = 'John Doe';

SQL: Foreign key references a composite primary key

I'm new to SQL and there are a lot of things going on that I still don't seem to quite understand. I have the following table
CREATE TABLE Person
(
First_Name varchar(20) NOT NULL,
Name varchar(20) NOT NULL,
Address varchar(50) NOT NULL,
PRIMARY KEY (First_Name, Name, Address)
);
I know want to create another table that has the primary key from the table Person as foreign key and also as primary key:
CREATE TABLE Purchase
(
No_Installments int,
Rate int,
Person varchar(50) NOT NULL PRIMARY KEY,
CONSTRAINT PFK
FOREIGN KEY (Person) REFERENCES Person (First_Name, Name, Address)
);
For some reason this doesn't work and I get an error every time. I've already looked up the other threads here on stackoverflow, but they don't really seem to help me. What am I doing wrong?
If you have a compound PK made up from three columns, then any child table that wants to establish a foreign key relationship must ALSO have all those 3 columns and use all 3 columns to establish the FK relationship.
FK-PK relationship is an all or nothing proposal - you cannot reference only parts of a primary key - either you reference all columns - or you don't reference.
CREATE TABLE Purchase
(
No_Installments int,
Rate int,
Person varchar(50) NOT NULL PRIMARY KEY,
First_Name varchar(20) NOT NULL,
Name varchar(20) NOT NULL,
Address varchar(50) NOT NULL,
CONSTRAINT PFK
FOREIGN KEY (First_Name, Name, Address)
REFERENCES Person (First_Name, Name, Address)
);
Have an integer primary key, using identity, auto_increment, serial or whatever for your database:
CREATE TABLE Person (
PersonId int identity PRIMARY KEY
First_Name varchar(20) NOT NULL,
Name varchar(20) NOT NULL,
Address varchar(50) NOT NULL,
CONSTRAINT unq_person_3 UNIQUE (First_Name, Name, Address)
);
Then use the identity column for the reference:
CREATE TABLE Purchase (
PurchaseId int identity PRIMARY KEY,
No_Installments int,
Rate int,
PersonId int,
CONSTRAINT PFK
FOREIGN KEY (PersonId) REFERENCES Person (PersonId)
);
Notes:
You really don't want to have to deal with a composite primary key. Have you thought about what the joins will look like?
You don't want a primary key where the values are subject to change. What happens when someone changes his/her name? When someone moves?
Person should not be the primary key in Purchases. Are you only allowing someone to make one purchase?
As noted initially, how you generate such a column varies by database; identity happens to be the way that SQL Server does this.
You probably want to assign a unique ID to each person, not relying on their name to be unique or for an address to be required. That ID will be your Primary key and foreign key. Your purchase table should also have its own id for its primary key -- otherwise because primary keys must be unique, each person can only have one purchase.
CREATE TABLE Person (
id serial NOT NULL,
First_Name varchar(20) NOT NULL,
Name varchar(20) NOT NULL,
Address varchar(50) NOT NULL,
PRIMARY KEY (id));
CREATE TABLE Purchase (
id serial NOT NULL,
No_Installments int,
Rate int,
Person int NOT NULL,
FOREIGN KEY (Person) REFERENCES Person (id),
PRIMARY KEY (id));

SQL Multiple Foreign Keys as Primary Keys

If I declare the table below does it implicitly imply that both the foreign keys make a unique primary key or do I need to do something more to make both attributes as a primary key?
CREATE TABLE Report_has_Items
(
ReportID int REFERENCES Report(ReportID) NOT NULL,
ItemID int REFERENCES Item(ItemID) NOT NULL
)
Essentially both attributes which are foreign keys from other tables, together would form a unique key.
No it doesn't. The above table has no primary key. If you want to use the fields as a primary key use:
CREATE TABLE Report_has_Items(
ReportID int REFERENCES Report(ReportID) NOT NULL,
ItemID int REFERENCES Item(ItemID) NOT NULL,
PRIMARY KEY (ReportID, ItemID)
)
or something similar depending on your sql dilect.
Let's name our constraints, eh?
CREATE TABLE dbo.Report_has_Items(
ReportID int NOT NULL,
CONSTRAINT [FK_RHI_Report] (ReportId) REFERENCES dbo.Report(ReportID),
ItemID int NOT NULL,
Constraint [FK_RHI_Item] (ItemId) REFERENCES dbo.Item(ItemID),
CONSTRAINT [PK_RHI] PRIMARY KEY (ReportID, ItemID)
)
I am not sure i understand your question completely but i assume you are trying to create a composite primary key (primary key with more than one attribute). You could do the following.
CREATE TABLE Report_has_Items(
ReportID int references Report(ReportID),
ItemID int references Item(ItemID),
PRIMARY KEY (ReportID , ItemID )
);
Note:The pair (ReportID , ItemID ) must then be unique for the table and neither value can be NULL.
Here is a very useful link for SQL Queries