SQL Inserts sql Developer - sql

I am using Oracale SQL Developer, and I am wondering when I insert data into a table, will the data cascade to another table that has the first tables primary key as a foreign key for example if I run
insert into DEPARTMENT values ('1', 'Karate');
Will the first value in Column 'DEPARTMENTID' also be inserted into the other table I.E 'TEACHER' or do I have to manually insert the value into the both tables.
Thanks

No, inserts will not cascade. You'll have to insert data in parent table and then in child tables.

Related

Duplicate key in foreign table

I have these Oracle tables:
CREATE TABLE AGENT(
ID INTEGER NOT NULL,
GROUP_ID NUMBER(38,0),
........
)
/
CREATE INDEX IX_RELATIONSHIP_AG_GROUP ON AGENT (GROUP_ID)
/
ALTER TABLE AGENT ADD CONSTRAINT AGENTID PRIMARY KEY (ID)
/
CREATE TABLE AGENT_CONFIG(
AGENT_CONFIG_ID INTEGER NOT NULL,
AGENT_ID INTEGER,
.......
)
/
CREATE INDEX IX_RELATIONSHIP16 ON AGENT_CONFIG (AGENT_ID)
/
ALTER TABLE AGENT_CONFIG ADD CONSTRAINT KEY17 PRIMARY KEY (AGENT_CONFIG_ID)
/
ALTER TABLE AGENT_CONFIG ADD CONSTRAINT RELATIONSHIP16 FOREIGN KEY (AGENT_ID) REFERENCES AGENT (ID)
/
I want to use the first table AGENT as main table to store data. Into the second table I want to store configuration about AGENT and use AGENT ID as unique ID.
But I have this problem: I insert successfully row into table AGENT. I need to also add empty row with the same ID into table AGENT_CONFIG and later update that row. How I can duplicate this ID value duplication? Probably with Oracle table trigger? Is three any other way like special relation between the tables?
I use this SQL Query for insert into AGENT table:
INSERT INTO AGENT ("ID, GROUP_ID, NAME.....) VALUES (AGENT_SEQ.nextval, ?, ......)
Is this correct query:
INSERT INTO AGENT (ID, GROUP_ID......) VALUES (AGENT_SEQ.nextval, ?.......)
RETURNING id INTO INSERT INTO AGENT_CONFIG (AGENT_SEQ.nextval, Agent_ID) VALUES (id)"
For this use currval pseudocolumn (as #Nicholas Krasnov specified) of the sequence that you are using after you use nextval for table "AGENT". Currval will duplicate the value that you used for table "AGENT".
So for example :
1. INSERT INTO AGENT (ID, GROUP_ID......) VALUES (AGENT_SEQ.nextval, .......)
2. INSERT INTO AGENT_CONFIG (AGENT_CONFIG_ID,AGENT_ID) VALUES (NULL,AGENT_SEQ.CURRVAL) ;
Link for Sequence Pseudocolumns --> https://docs.oracle.com/cd/B28359_01/server.111/b28286/pseudocolumns002.htm
To establish a 1:1 or 1:{1,n} relation is not that easy:
On every insert in the parent table you'd have to make sure an according insert in the child table is also made.
On every delete from the parent table you'd have to make sure to delete the according child records (usually via cascading delete or by mere foreign key constraints).
On every delete from the child table you'd have to make sure it's not the last one for its parent. (And in that case either forbid the delete or delete the parent with it.)
Forbid or react on updates to the IDs.
You can solve this by disabling (i.e. not granting) direct inserts and deletes (and maybe updates) on the table and provide PL/SQL functions to handle these instead.
If you are okay, however, with a 1:{0,1} or 1:{0,n} relation, then you can simply write the parent record and then look up its ID to write the child record(s).

inserting unique primary key exception

I was trying to insert records to a table using statement like
insert into table
(table_id, xxx,xxx)
values
(100,xxx,xxx)
and get the unique violation on the primary key.
and i did a select
select * from table where table_id = 100;
There is no record there.Thought insert before then, delete record.
And if i dont insert the table id, i got a null violation.
I was wondering if there is anyway to deal with this(maybe sequence)? I need to insert several thousands records using the inserting statement. some hundered have this prob.
Thanks in Advance!

How can I insert into a table that is connected to another table with a 1-1 relationship

I dont know much about SQL, and I've got a problem using it.
I have two tables that connected to each other 1-1
Tbl1 (int_id1, str_desc1,....) and
Tbl2 (int_id2, str_desc2,....)
And these two are connected to each other
int_id1 ---- int_id2
First I want to know that is my design true?
And how can I insert into one of these of two together.
Cause I've got problem when I try to insert into one
here's the error description:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_Tbl2_Tbl1". The conflict occurred in database "project", table
"dbo.Tbl1", column 'int_id1'.
Tnx...
It means you are trying to insert a value in a Foreign Key Column which does not exist in the Primary Key Column which it referencing to.
Any value you add in a Foreign Key Column, It must Exist in the Primary Key Column to which it referencing to, after all that is the whole Idea of adding Foreign Key Constraints. so you will not end up having orphan records in a table and also it reduces data redundancy.
Read Here for more information about Foreign Key Constraints.
First I want to know that is my design true?
When you have tables in a one to one relationship, the first question should be: why not just use one table? There may not be a need to separate the data.
Assuming you have 2 tables that have a 1to1 relationship. You can use a transaction to add rows to each. Lets say you have a Classes and Professors table, with a 1to1 relationship. Then you would do something like this:
begin tran;
insert into Classes (title) values ('Math 101');
insert into Professors(name) values ('Tim Rogers');
commit tran;
begin tran;
insert into Classes (title) values ('History 101');
insert into Professors(name) values ('Suzanne Bethany');
insert into Classes (title) values ('PE 101');
insert into Professors(name) values ('Emily Williams');
commit tran;
select * from Classes;
select * from Professors;

Copying a 1-to-many relationship between two tables to another two tables (SQL 2005)

I have two tables with a parent-child relationship. I would like to copy some of their records to two other tables, also with a parent-child relationship, but with a slightly different table structure.
There is a foreign key involved with both sets of tables, an integer column. All tables have the identity increment on for their primary key columns.
If I do a SELECT INTO from the source parent table to the destination parent table, the primary key values for the destination records will be different from the source records, and the parent-child relationship will be lost.
Does anyone know how I can preserve this relationship during the copy, given that I'll have new primary key values in the new parent table? I'd prefer not to set the identity increment off for the new tables during this copy procedure, because there's no guarantee the primary key values in the source table won't already be in the destination.
Hope my description makes sense, and thanks for your opinions. Let me know if I can clarify further.
When I have done this, I did so by preserving the old ID after the insert into the new table.
Some people add a temporary column with the old ID, but I know of a better way.
You can use the OUTPUT clause to insert the inserted records into a temp table (with the new and the old IDs as a mapping table. Then join to that when inserting the child records.
Something like this (crude)
DECLARE #MyTableVar TABLE (
NewID INT,
OldID INT
)
INSERT NewParentTable
OUTPUT
INSERTED.ID,
old.ID
INTO #MyTableVar
SELECT *
FROM OldParentTable old
INSERT NewChildTable
SELECT
old.ID,
t.NewID,
old.name
FROM OldChildTable old
INNER JOIN #MyTableVar t ON t.OldID = old.ParentID
You'd have to maintain the original ID in a separate column in the table with the corresponding parentID.
So in your destination table, you'd need to add an parentID_orig column (not auto-number) to retain the link.

MySQL: Can I constraint column values in one table to values in a column in another table, by DB design only?

Example:
Table "persons", Column "surname" may only contain values predefined in
Table "names", Column "surnames", which would contain a collection of surnames acceptable for the purpose.
Can I achieve this by design (i.e. without involving any validation code)? On a MyISAM table? No? On InnoDB?
Thank you.
What you're asking for is a foreign key constraint. You'd need to use InnoDB - quote:
For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it.
To add a foreign key constraint within the CREATE TABLE statement for PERSONS:
FOREIGN KEY (surname) REFERENCES names(surnames)
Using an ALTER TABLE statement if the tables already exist:
ALTER TABLE persons
ADD CONSTRAINT FOREIGN KEY (surname) REFERENCES names(surname)
Be aware that if you use the ALTER TABLE statement, the data in the PERSONS table can only contain surname values that exist in the NAMES.surname table - it can not be applied until after the data has been fixed.
For MyISAM tables you can achieve desired functionality by using triggers.
For instance (validate insert),
DELIMITER //
CREATE DEFINER=`root`#`localhost` TRIGGER BEFORE INSERT ON persons
FOR EACH ROW
BEGIN
DECLARE tmp_surname varchar(100);
SELECT surname into tmp_surname FROM names WHERE surname = NEW.surname;
IF (tmp_surname IS NULL) THEN
INSERT INTO t1(id,value) VALUES('aaa'); #raise an 'exception'
END IF;
END;//
delimiter;
Mysql doesn't have exceptions, but you can terminate execution(and, consequently, 'rollback' changes) by creating an invalid statement