Add tags in data base (split many to many relation-ship Sql ) - sql

I need to add a tags for 3 tables in my relational data base ! So adding tags as a table is not the best solution, because I'll be obliged to create 3 tables for the 3 different tables existant
What is the optimised solution that I can use ?

Just use those tags in One Main Tag Table and make those tags as foreign keys to your existing three tables primary keys like
CREATE TABLE Main_Tag
(ID_table1 varchar(10),
ID_table2 varchar(10),
ID_table3 varchar(10),
FOREIGN KEY (ID_table1) references
Table1(Id_tab1),
FOREIGN KEY (ID_table2) references
Table2(Id_table2),
FOREIGN KEY (ID_table3) references
Table3(Id_tab3));

Related

How do I create a bridge table? Do I create two primary keys and two foreign keys?

I have a table that I want to make which is a bridge table for teacher and class table. This is the ERD
I initially thought I'm going to create this table by
CREATE TABLE class_teacher
(
teacher_id number(3),
class_id number(2),
CONSTRAINT class_teacher_pk
PRIMARY KEY(teacher_id, class_id),
CONSTRAINT class_teacher_teacher_fk
FOREIGN KEY(teacher_id) REFERENCES teacher(teacher_id),
CONSTRAINT class_teacher_class_fk
FOREIGN KEY(class_id) REFERENCES class(class_id)
);
But on the web I see people just having two foreign keys and no primary key, or table with no foreign key and having a primary key for two columns.
Am I doing it incorrectly?
Am I doing it incorrectly?
No, it looks correct.
Although I would question the size of the numeric data types as you are restricted to only have 1999 teachers and 199 classes (including negative numbers); this may be enough for immediate use but after several years when classes get re-organised or when the syllabus is re-written and new classes are created then you may run out of primary keys.
Does create statement returns any error? Otherwise you should be good.
Try to insert some data in all 3 tables and run some delete statements to see how it goes.

SQL: Relation between different rows of two tables

I'm creating er-diagram for "Airport" database. And I got stick with tables "Flight" and "Airport" .
In table "Flight" I have 2 columns: "Departure_place" and "Arrival_ place". Both of it should be connected to values in table "Airport".
So, How can I make that in the proper way on er-diagram and on sql?
I think you are looking for foreign keys. Here is a tutorial for that:
https://www.w3schools.com/sql/sql_foreignkey.asp
In your case first create the tables:
create table flight(
idFlight int primary key,
idDeparturePlace int,
idArrivalPlace int);
create table airport(
idAirport int primary key);
Now you have to add tow foreign keys. One for idDepaturePlace and one for idArrivalPlace.
Follow this syntax:
ALTER TABLE <child table> ADD FOREIGN KEY (<col in child table>) REFERENCES <master table>(<col in master table>);
In your case you have to add this both keys to your flight table:
ALTER TABLE flight ADD FOREIGN KEY (idDepaturePlace) REFERENCES airport(idAirport);
ALTER TABLE flight ADD FOREIGN KEY (idArrivalPlace) REFERENCES airport(idAirport);

How to setup tables with varying number of Foreign Key of specific type

Say I have three tables.
MenuItem(MenuItemID{PK}, MenuItemName, MenuItemIngredient)
Ingredient(IngredientID{PK}, IngredientName, SupplierID)
Supplier(SupplierID{PK}, SupplierName)
Each MenuItem in the MenuItem table can have multiple ingredients.
Each Ingredient in the Ingredient table can have multiple suppliers.
My current way of doing this is to have two extra tables.
MenuIngredientLink(MenuItemID{PK}, IngredientID{FK})
IngredientSupplierLink(IngredientID{PK}, SupplierID{FK})
Then remove the last column on MenuItem and Ingredient tables.
This doesn't really seem like a good way to do it though.
Any alternatives?
Fairly new to SQL so any help would be appreciated.
Using Microsoft SQL Server 2014.
Since everything follows a strict one-to-many relationship,your original approach to the problem is correct.So no need to introduce two mapping tables.
However, a problem arises in a scenario like this. An ingredient is supplied by many suppliers as well as a supplier provides multiple ingredients, creating a many-to-many relationship.Then your approach to a mapping table is the preferred solution.
I have included an external source for your reference. Hope this helps.
Managing Database Relationships
We can make tables with foreign keys as you've specified. If you start with the Supplier table, you should insert all values required, and it has no foreign key so you have no restrictions. Next, you insert values into the Ingredients table, since it requires a foreign key to Suppliers. Lastly, you insert values into the MenuItem table, since it has a foreign key to Ingredient. You can create the tables first, then add the foreign keys later as I've written below.
--Suppliers does not have any foreign keys
CREATE TABLE Supplier (SupplierID int PRIMARY KEY, SupplierName varchar(100))
--Ingredients has a foreign key to Supplier(SupplierID)
CREATE TABLE Ingredient (IngredientID int PRIMARY KEY
, IngredientName varchar(100)
, SupplierID int)
ALTER TABLE Ingredient ADD FOREIGN KEY (SupplierID) REFERENCES Supplier(SupplierID)
--MenuItem has a foreign key to Ingredient(IngredientID)
CREATE TABLE MenuItem (MenuItemID int PRIMARY KEY
, MenuItemName varchar(100)
, MenuItemIngredientID int)
ALTER TABLE MenuItem ADD FOREIGN KEY (MenuItemIngredientID) REFERENCES Ingredient(IngredientID)

Adding multiple foreign keys in column

Is there a way to store multiple values in a column that has a foreign key constraint?
Let's say I have a states table and a project table. Project table has a foreign key constraint with states table. Now we are implementing the same project in three different states. How can I select multiple states?
Sample
Create table states (
Stateid int identity primary key,
State varchar(100)
);
Projects Table
Create table projects (
ProjId int primary key identity,
ProjTitle varchar (100),
Budget decimal,
);
How can I insert multiple values in projects states table?
Based on TPHE answer lets me create another table called projectstates
Create table projectstates(
projStatid int identity primary key,
stateid int,
ProjId int
constraint fk_ProjId foreign key (ProjId) references Projects(ProjId),
constraint fk_stateid foreign key (stateid) references states(StateId)
);
Now how can i insert data in ProjectStates while adding project to the project table?
The best way I've found to do this is to create the second table with no foreign key constraint at first. Then you can populate both tables with the data, then introduce the constraint afterwards - assuming the data in the tables complies with the constraint.
Also, if a many to many relationship exists, add in a mapping table to allow this.
It would break some basic rules of database design to add multiple values. You should create a new table that has a one to many relationship with the states table (each state can have multiple values in the new table) and contains a column for the associated project IDs (also with a one-to-many relationship). Then you would join from the states table to the new table and then to the projects table or vice versa. More info on how and why to design databases in this way.

How to create linking/joining table in Oracle database

I'm creating a mock up database for the first time. I have created the Relational Model which consists of a many to many relationship. In the Relational Model it has a separate linking/joining table. When creating the database do I need to create this linking table as a separate table also? Or can I just put each foreign keys in the many to many tables?
If I need a separate table how do I link these tables together via syntax?
Thanks
We can only build foreign keys in a one-to-many fashion. So you need this intersection table. It is the sort of additional construct we introduce when transforming a logical data model into a physical one.
The intersection table often has just two columns, the referencing keys of the two tables you want to link in M:N fashion (there may also be some metadata columns to hold properties of the link). It usually has a compound primary key on the two columns, to avoid redundancy. It has a foreign key on each of the referenced tables, which must have defined primary keys on the referenced columns.
The syntax is pretty obvious; this sample builds two master tables and an intersection defining just the keys.
create table m1 (
id number not null
, constraint m1_pk primary key (id) );
create table m2 (
id number not null
, constraint m2_pk primary key (id) );
create table intersection_t (
m1_id number not null
, m2_id number not null
, constraint int_pk primary key (m1_id, m2_id)
, constraint int_m1_fk foreign key (m1_id)
references m1 (id)
, constraint int_m2_fk foreign key (m2_id)
references m2 (id)
);