How to insert data into a relation table correctly in SQL? - sql

I have some data in a general table called ImportH. The data has been imposted from a csv file. I have also created two tables, Media and Host (each one has it's respective ID. These tables are related by a third table called HostMedia.
Each Host can have (or not) different types of Media (facebook, email, phone...).
I'll provide some images of the tables:
Table ImportH
Table Host
Table Media
How can I insert the data from the other tables into table HostMedia? This table looks like this:
create table HostMedia (
host_id int references Host (host_id),
id_media int references Media (id_verification),
primary key (host_id, id_media)
);
I have tried this:
insert into HostMedia (host_id, id_media)
select Host.host_id, Media.id_verification
from Host, Media;
But this does the cartesian product for all the hosts assigning them all the rows on the Media table. What's the correct way?

The "media" column in your "ImportH" table looks almost like a valid JSON, so this might work:
INSERT INTO HostMedia (host_id, id_media)
SELECT i.host_id, m.id_verification
FROM (
SELECT host_id,
json_array_elements_text(replace(media,'''','"')::json) AS media_name
FROM ImportH
) AS i
JOIN Media AS m ON m.media = i.media_name;
Notes: it would be easier if you
provided text data instead of screenshots
used logical column names

Related

Adding column while loading csv in SQL

I have an existing table (NameList) in to which I would like to load the contents of multiple csv files (fileA.csv, fileB.csv ...). The columns of the table are identical to those of the csv except that I want to record for each row the id of the csv file it came from. The id would be taken from another table which contains the properties of each file.
The table with the list of files would look like this:
CREATE TABLE files
(
id serial,
fileName varchar(128),
path varchar(256),
PRIMARY KEY (id)
)
The table to insert the csv contents in to would look like:
CREATE TABLE NameList
(
FirstName varchar(40),
LastName varchar(40),
SourceFile_ID int,
FOREIGN KEY (SourceFile_ID) REFERENCES files(id)
)
The csv files would look as follows:
Name of file:
fileA.csv
Contents:
FirstName,LastName
John,Smith
.
.
.
The only thing relating to this I could find so far is this:
Add extra column while importing csv data in table in SQL server table
However they suggest to use a default value on the additional column which would not solve my problem since I need to have a different value for each file I add.
You could insert the data into a temporary table (https://www.postgresqltutorial.com/postgresql-temporary-table), update the column, then move the data to the main table.
This would avoid problems with 2 CSVs being loaded at once, because they'd be using different temp tables (as long as 2 different db sessions are used for the inserts). Even if only one session is used, you could have different names for the temp table for different CSVs.

Inserting data from another table - Oracle SQL

I need to insert data into a media table. The data must have the media id (which is a sequence), format (DVD, VHS) and the movie title id which is a sequence that exists another table named movies. How do I pull the data for the title_id from the movies table into the media table? I'm not quite sure where to start but I've listed the code I have so far for the first 2 columns.
INSERT INTO m_media
(media_id, format, title_id)
VALUES (media_id_seq.NEXTVAL, 'DVD', );
Instead of inserting separate values, use SELECT statement which will fetch data from the movies table. Something like this:
insert into m_media (media_id, format, title_id)
select media_id_seq.nextval,
'DVD',
m.title_id
from movies m
where ... --> condition, if you want to restrict rows returned by that SELECT

How to display the list of books of a user in php code

I am a beginner in programming.
I want to create a site in which I would like to display the list of books of a user that he has previously added. here is the schema of the code.
table book;
id ,name,detail ,urlimage.
table users;
id,name,email,password.
table add_book;
uses_id INT ,book_id INT , key primary (users_id ,book_id ).
I have three tables in my database; the book table, the users table and the add_book table. their structures is given in the above question add_book.users_id and add_book.book_id are foreign keys. I would like to make sure that when a user adds a book to his list, an entry is created in the add_book table with the request. ('INSERT INTO kal224_sory.add_book (users_id, book_id) VALUES (: users,: book)'); $ sql-> execute (['users' => $ users_id, 'book' => $ book_id]); .It works well where there is problem, I try to send in json the fields of the table book that correspond to book_id which have the same entries users_id where(add_book.users_id = $ users_id) of the table add_book. the code I tried is;
You need to SELECT the data from the add_book table that belongs to a particular uses_id (sp.)
SELECT * FROM add_book WHERE uses_id = 1 -- change '1' to whatever user's ID you'd like to view data for

Oracle: selfcopying data from Oracle tables

Application has different versions. Each version has it's own set of values in each table. I need to provide functionality to copy data from one version to another. Problem :
By inserting data I am trying to insert Ids which has already been in use in this table. So, I need to change ids of components which I want to insert but I must save relationship between those components. How cat I do that?
Create a master table which has a surrogate key as your primary key. A numeric value of type NUMBER(9) works well. You can create a sequence and trigger to automatically insert this.
The rest of the table is the column of your current table plus a column to indicate which version the row is for.
For simplicity you may wish to create views on top of the table along the lines of
select * from master_table where version_id = ####;
To copy the data from one version to another this will work:
Insert into master_table seq_master_table.nextval, new version_id,.....
from master_table
where version_id = ####;

How can I create a table that only allows data to insert if they are allowed

How can i create a table, that allows only to put data in NAME, if the data matches with the data that i want to be allowed in NAME. So like Bla1 or Bla2.
CREATE TABLE Table1 (
NAME VARCHAR(23)
NAME has to be one of them: ('Bla1', 'Bla2')
)
The best way to do it is probably to have a second table with all the allowed names in it, and making a FOREIGN KEY from the name field in your Table1 to the name field in that other table. That'll automatically fail any insert queries for which the name is not contained in the list of allowed names.
This has an advantage over things like ENUM and such in that it does not require you to rebuild your table (which is a very expensive operation) every time you want to allow another name and it also allows you to later add additional related info to each name by adding it to the other table.
Here's a great article on why using a foreign key is much better than using enums or other such checks in the table itself: http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/
Try this:
CREATE TABLE Table1 (
name VARCHAR(23) CHECK( name IN ('Bla1','Bla2') )
);