Drop one to one relationship tables - sql

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

Related

SQL Alter Table Foreign key On Delete

I have a table in my database called trains with these columns:
id, name, station.
Station is the foreign key to other table stations. When I delete some specific row from stations, since trains references to this table, I get an error. Hence, what I try to do is to do alter table on trains and add on delete set null attribute to station column, like this:
alter table trains alter column station on delete set null;
However, this seems to be the wrong syntax and I can't find the right one, which can make this without removing the whole table and creating it again (so I need to use this alter command).
If you are using SQL Server, then you cannot alter a foreign key constraint to add on delete. First drop the constraint and then create a new one.
ALTER TABLE trains
DROP CONSTRAINT constraintname;
ALTER TABLE trains
ADD CONSTRAINT fk_station
FOREIGN KEY (child_column_name)
REFERENCES stations(parent_column_name)
ON DELETE CASCADE;

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

Dropping a table in Oracle SQL

Whenever I try to drop a table or create a table it is showing these errors:
DROP TABLE SUBURB;
DROP TABLE STOCKITEM;
DROP TABLE MANUFACTURER;
DROP TABLE WAREHOUSE;
DROP TABLE CITY;
DROP TABLE STATE;
Error at line 1: ORA-02449: unique/primary keys in table referenced
by foreign keys
CREATE TABLE STATE (
statecode varchar(3)
,statename varchar(30)
,population number(8)
,primary key(statecode)
);
Error at line 1: ORA-00955: name is already used by an existing object
Can anybody explain why this happens?
If you're really sure you want to drop the table even though it's referenced in foreign keys you can force it like this:
drop table state cascade constraints;
This syntax is defined in the Oracle SQL Reference.
Note that this drops any foreign key relationships. So you will need to recreate them after you have rebuilt the table (and its primary key). Normally this is okay because the most common use case is trashing and re-creating schemas in Development or CI environments.
We can use cascade constraints to make our build scripts easier to maintain. There are two alternatives:
Explicitly drop the foreign key constraints before dropping the
tables, either with a script or with dynamic SQL.
Order the DROP
TABLE statements so that dependent tables are zapped first, along
with their pesky foreign keys. Easy enough for a handful of tables,
more painful with a large schema.
You can use below query to fetch the references of table which should be dropped before dropping the table.
select table_name, constraint_name, status, owner
from dba_constraints
where 1=1
--and r_owner = :p_owner --if you know schema
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from dba_constraints
where constraint_type in ('P','U')
and lower(table_name) = lower(:p_table_name)
--and r_owner = :p_owner
)
order by table_name, constraint_name
if you create the primary key and also create the foreign key than you cannot drop the table
you drop the table in this way for example if you have the table of students or teachers you want to drop this table you should write
DROP TABLE students CASCADE CONSTRAINTS;
and also you drop the table of teachers
DROP TABLE teachers CASCADE CONSTRAINTS;
SUBURB
Table is a parent table for any other table First you drop the child table then you can drop the SUBURB table....
And a table named as STATE is already present in your database...so you cant create the table having the same name....once if you drop the STATE table you can create another....
here's the solution that works for me fine in Oracle sample database :
DROP TABLE ['Your_Table_Name'] STATE CASCADE CONSTRAINTS;

SSIS Lookup multiple dates in my fact table, how to pull this off using one date dimension?

In my SSIS/DW project, I have a DIM.DATE dimension which is linked to my FACT table by a surrogate key as follows:
ALTER TABLE FACT.SALES ADD date_id INT NOT NULL
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_dim_date FOREIGN KEY (date_id) REFERENCES DIM.DATE(date_id)
This creates a "date_id" in my fact table, now during my SSIS import process I have a date column being passed (shipped_date), I use this to look up the DIM.DATE table and pass in the surrogate key in my dimension.
This works great, but now I need to have a few different date dimensions for invoice date, received date, etc.
I am confused as to how to make use of the existing DIM.DATE to do this?
I could then add more columns into my fact table..
-- add column into fact table
ALTER TABLE FACT.SALES ADD shipped_date_id INT NOT NULL
ALTER TABLE FACT.SALES ADD invoice_date_id INT NOT NULL
-- add foreign key
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_shipped_date FOREIGN KEY (shipped_date_id) REFERENCES DIM.DATE(date_id)
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_invoice_date FOREIGN KEY (invoice_date_id) REFERENCES DIM.DATE(date_id)
But when I do my lookup, I can only pass in the "date_id" column.. I am confused how to make this work all together.
Anyone able to clear this up for me?
You have to use multiple LookUp transforms... 1 for each DateKey field in the fact table.

How to add surrogate key to related tables?

I need to add a auto-inc surrogate key to three tables in my data warehouse:
Note: These are not the real table names
JakMaster (JakMasterId, Date) (PK) Note: JakMasterId is varchar(60)
JakToRoad (JakMasterId, Date) (FK)
JakToBig (JakMasterId, Date) (FK)
What steps should I take to add a surrogate key to these three tables so that the new keys reference each other correctly?
Thanks!
I'm assuming you are wanting to replace JakMasterId with an auto-increment field so the other two tables don't need a varchar(60) field and to improve query times, but that you are keeping JakMasterId as information.
-- set database single-user
-- drop foreign keys
create table NewMaster (ID int identity(1, 1), JakMasterId, Date))
insert NewMaster(JakMasterId, Date) select JakMasterId, Date from JakMaster
drop table JakMaster
sp_rename 'NewMaster', 'JakMaster'
alter table JakToRoad add MasterId int
alter table JakToBig add MasterId int
update JakToRoad set MasterId = JakMaster.ID
from JakToRoad
inner join JakMaster on JakMaster.JakMasterId = JakToRoad.JakMasterId
update JakToBig set MasterId = JakMaster.ID
from JakToBig
inner join JakMaster on JakMaster.JakMasterId = JakToBig .JakMasterId
alter table JakToRoad drop column JakMasterId
alter table JakToBig drop column JakMasterId
alter table JakToRoad add constraint FK_JTRtoJM foreign key (MasterId) references JakMaster (ID)
alter table JakToBig add constraint FK_JTBtoJM foreign key (MasterId) references JakMaster (ID)
-- reset database to multi-user
You might be able to do this in three steps within the DB to create the surrogate PK
Alter the table to create the surrogate key column. Except, it has to be nullable.
Write a small program to set the key values. It's a loop doing UPDATEs.
Alter the table to make the surrogate key column non-null, auto-increment, indexed, unique, etc.
Now you need to create the FK's.
Alter the table to add the FK column. Again, it must be nullable.
Write a small program to set the FK column. This is a SELECT (to get the PK row based on non-surrogate keys), and an UPDATE to the referencing table.
If necessary, alter the table to make the FK non-null. This isn't always necessary, it depends on the definition of the FK table's relationship with the PK table.
Repeat the FK creation for all other tables.