SQL: Relation between different rows of two tables - sql

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

Related

Add tags in data base (split many to many relation-ship 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));

How to create associative table where one of the fields is not primary key?

I am structuring a postgres database.
I have two tables, products (coke) and optional (with ice, lemon ...), that is, a relationship many to many.
An associative table is usually built using the primary keys of the tables, correct? However, in my case, there is a specific feature ... due to some imports from other databases...I have two ids fields (id and "externalId"), one primary key and one common ... one is the local id of my bank and the other represents the id that the item has in the bank from which it was imported.
I need an associative table between "externalId" and a primary key from another table.
ExternalId is not a primary key...
ALTER TABLE public.opcional_produto
Add
CONSTRAINT idproduto_fkey FOREIGN KEY (prod_id) REFERENCES public.produto (prod_idExt)
ERROR: there is no unique constraint matching given keys for
referenced table "produto" SQL state: 42830
How can I do?
If externalid is unique, you should create a unique constraint:
ALTER TABLE produto ADD UNIQUE (externalid);
Ideally it should also be not nullable:
ALTER TABLE produto ALTER externalid SET NOT NULL;
Now it can be used as target of a foreign key.

Drop one to one relationship tables

I created two tables which are 1 to 1.
So one table has foreign key from another.
How can I drop the tables? I did not use on delete cascade while creating tables.
So I either have to somehow change it, or IDK..
I have done this.
CREATE TABLE hotel(
id_hotel
...
)
CREATE TABLE Manager(
ID_Manager
...
id_hotel FOREIGN KEY ...
)
and then I added
ALTER TABLE Hotel ADD id_manager INT NOT NULL;
ALTER TABLE Hotel ADD FOREIGN KEY (id_manager) REFERENCES Manager(id_manager);
You first have to drop the table with the foreign key in the column, eg: If you have 2 entities one named driving_license and the other person, and you store the id from person in the table driving_license, you have to first drop the table driving_license
Or if you are using MySQL: You can write:
SET_FOREIGN_KEY_CHECKS=0;
//Drop however you want
SET_FOREIGN_KEY_CHECKS=1
For MSSQL you can use:
ALTER TABLE <yourtablename> NOCHECK CONSTRAINT ALL
And then drop your table

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.

Creating relation between tables in Movie Database

I have created a movie database that contain following tables :
1.Film
2.People
3.Genres
4.Role
5.Users
Here is my sql
create table Film ( id INT(30),title varchar(30),images varchar(30)
primary key (id));
create table people(id INT(30),Fname varchar(30),Lname varchar(30),
primary key (id));
create table Role(id INT(30), name varchar(30),
primary key(id));
i want create relation between Film,People and Role table.SO my Question is do i need to create a table to make relation between those table and is it necessary to use auto_increment for id column?
You'd want to create some tables like:
FilmRole( FilmId INT, RoleId INT) these 2 columns would make your PK and they are also FK's to their
FilmPeople (FilmId INT, PeopleId INT) respective source tables.
FilmUsers( FilmId INT, UserId INT)
You could add a single IDENTITY (for SQL Server for example) column to each table if you wanted but in this particular case a 2 column PK is adequate as these tables simply point to other records.
You need to alter your table and add in a foreign key (Primary key in one table and attribute in another). Examples how to do it here! http://www.w3schools.com/sql/sql_foreignkey.asp
do i need to create a table to make relation between those table ?
YES ! to enforce Referential integrity read this
is it necessary to use auto_increment for id column?
Depends but it is most prefered way of creating a PK on a table