Insert data from one SQL Server table into another - sql

I have to insert data from one table into another. The problem is that the table where the data has to be inserted into has a primary key.
Suppose there are 2 tables A and B.
A has two columns id (primary key) and name
B has two columns id and name
How can we insert data from table B into table A with the primary key column?

For example there are 2 tables A & B as you stated.
insert into A (id,name) values(2, 'Testing');
insert into B (id, foreign_key, name) values(1, 2, 'Hello');
Both tables have a primary key. Primary key of table A is used as a foreign key to connect to the table B so that they interact with each other
Here is its design:

Related

Multiple insert statement with same variable

Is it possible to perform several SQL INSERT statements with the same value to multiple tables? For example, I am passing a ID and want to INSERT those into 4 tables. All tables share the same primary key, as table 2, table 3 and tabl 4 contain sub-properties of table1 elements.
INSERT INTO table1 (id)
VALUES ($phpVar);
INSERT INTO table 2 (id)
VALUES ($phpVar);
INSERT INTO table 3 (id)
VALUES ($phpVar);
INSERT INTO table 4 (id)
VALUES ($phpVar);
Further how could I bundle those into one statement, to prevent DB data failure during crashes etc?

Insert multiple values into the same row for a single foreign key

I'm creating a project for a hotel reservation, and I want to insert multiple values into a column which is a foreign key in a table that has a primary key column but it's not possible to insert a duplicate key when it's primary and it's not possible to insert different values than the values that inserted in the main column that I used it as foreign key in this table, so what can I do?
Clarification
I have a table for reservation and another table for hotel services, there is a column in [Reservation] table which is the primary key (ReservationID) and there is a column in the [Hotel_Services] table that I used as a foreign key to the [Reservation] table which is (ServiceID).
What I want is to insert multiple service IDs into each reservation to indicates the services that each guest has added to his reservation, how can I do this?
Table #1: Reservation
`ReservationID` (primary key) e.g 10,20,30
Table #2: Hotel_Services
`ServiceID` (foreign key to `Reservations`) e.g 1, 2, 3
Desired result: add multiple service IDs into each reservation
e.g:
Reservation 10 has 1,2 services
Reservation 20 has 1,3 services
Reservation 30 has 2,3 services
You need a table ReservationServices, defined something like this:
create table ReservationServices (
ReservationServiceId int identity primary key,
ReservationId int not null references reservations(reservationid),
ServiceId int not null references services(serviceid);
);
Then your data would look like:
ReservationServiceId ReservationId ServiceId
1 10 1
2 10 2
3 20 1
4 20 3
. . .
So, inserting a new service for a reservation is, well, as simple as inserting a new row in this table.

Can I make a unique constraint in Oracle on a combination of 2 columns from 2 different tables?

Column A is the PK of table A. Column B is a column in table B. Column C is a FK is table B that references column A in table A.
Can I define a constraint that says that that a column B AND column C have to be unique? As in, I don't want any repeats of the same combination of values from A and B.
Here's one possibility i'm thinking about:
Create a unique ID column.
unique_id varchar2 GENERATED ALWAYS AS (B || '-' || FK that references column A) VIRTUAL
set it as unique
CONSTRAINT unique_id UNIQUE
If i go with this solution i'm confused about one thing. The docs say: "Virtual columns are not supported for index-organized, external, object, cluster, or temporary tables." Obviously, because they don't get stored like other columns. Would this be a problem if I wanted to make my tables clustered?
Yes, you can create a UNIQUE constraint on two columns. For example:
create table table_a (
a number(6) primary key not null
);
create table table_b (
b number(6) not null,
c number(6) not null,
constraint uq1 unique (b, c),
constraint fk1 foreign key (c) references table_a (a)
);
Then, if you try to insert it will fail for duplicates. For example:
insert into table_a (a) values (1);
insert into table_a (a) values (2);
insert into table_b (b, c) values (10, 1);
insert into table_b (b, c) values (10, 1); -- fails!
insert into table_b (b, c) values (10, 2);
See running example at db<>fiddle.

No auto increment in my foreign key

I'm working in my school project and I want to make the id subject of any student incremented automatically as foreign key. I am showing you the example below as prototype, there are two tables, when I'm trying to insert data into the second table, I get an error (necessary to insert another field id of the table)
CREATE DATABASE database1;
USE database1;
CREATE TABLE table1
(
IdTable1 INT NOT NULL IDENTITY,
NOM VARCHAR(30),
PRIMARY KEY(IDMEDO)
);
--auto increment is working here
INSERT INTO table1
VALUES ('data1Table1'), ('data2Table1'), ('data3Table1');
--auto increment is working here just with the primary key
CREATE TABLE table2
(
IdTable2 INT not null IDENTITY,
IdTable1 INT,
dataTable2 varchar(30),
primary key(IdTable2),
constraint fk_tbl1 foreign key(IdTable1) references Table1
);
--necessary to add id field
INSERT INTO table2
VALUES ('data1Table2'), ('data2Table2'), ('data3Table2');
You should always (as a "best practice") define the columns you want to insert data into - that way, you can specify those that you have to provide values for, and let SQL Server handle the others. And for the foreign key, you have to explicitly provide a value - there's no "auto-magic" (or "auto-incrementing") way of associating a child row with its parent table - you have to provide the value in your INSERT statement.
So change your code to:
-- explicitly *specify* the NOM column here!
INSERT INTO table1 (NOM)
VALUES ('data1Table1'), ('data2Table1'), ('data3Table1');
-- again: explicitly *specify* the columns you want to insert into!
INSERT INTO table2 (IdTable1, dataTable2)
VALUES (1, 'data1Table2'), (2, 'data2Table2'), (3, 'data3Table2');
You need to insert the value of the foreign key wrt to table 1 now if you enter a value that's not in table 1 column which is acting as a foreign key then also it will not accept
Simply
Primary key and foreign key should match
Foreign key can't be null if table 1 doesn't contain a primary key with value null

Using references in MYSQL

I got an answer to another question here:
DB Schema For Chats?
It's a great answer, but I am not understanding the bit about references. I can do SQL statements but I have never used references.
What are they used for?
How are they used?
Give an example please
The REFERENCES keyword is part of a foreign key constraint and it causes MySQL to require that the value(s) in the specified column(s) of the referencing table are also present in the specified column(s) of the referenced table.
This prevents foreign keys from referencing ids that do not exist or were deleted, and it can optionally prevent you from deleting rows whilst they are still referenced.
A specific example is if every employee must belong to a department then you can add a foreign key constraint from employee.departmentid referencing department.id.
Run the following code to create two test tables tablea and tableb where the column a_id in tableb references the primary key of tablea. tablea is populated with a few rows.
CREATE TABLE tablea (
id INT PRIMARY KEY,
foo VARCHAR(100) NOT NULL
) Engine = InnoDB;
INSERT INTO tablea (id, foo) VALUES
(1, 'foo1'),
(2, 'foo2'),
(3, 'foo3');
CREATE TABLE tableb (
id INT PRIMARY KEY,
a_id INT NOT NULL,
bar VARCHAR(100) NOT NULL,
FOREIGN KEY fk_b_a_id (a_id) REFERENCES tablea (id)
) Engine = InnoDB;
Now try these commands:
INSERT INTO tableb (id, a_id, bar) VALUES (1, 2, 'bar1');
-- succeeds because there is a row in tablea with id 2
INSERT INTO tableb (id, a_id, bar) VALUES (2, 4, 'bar2');
-- fails because there is not a row in tablea with id 4
DELETE FROM tablea WHERE id = 1;
-- succeeds because there is no row in tableb which references this row
DELETE FROM tablea WHERE id = 2;
-- fails because there is a row in tableb which references this row
Important note: Both tables must be InnoDB tables or the constraint is ignored.
The REFERENCES keyword shows a foreign key constraint, which means that:
FOREIGN KEY (`chat_id` ) REFERENCES `chats`.`chat` (`id` )
...the chat_id column in the current table can only contain values that already exist in the chat table, id column.
For example, if the CHAT.id column contains:
id
----
a
b
c
..you can not add any values other than a/b/c into the chat_id column.