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
Related
I have a table that contains a list of xml tags/values that I need to use to join to another table to retrieve their actual value and display the result as a csv list.
Example varchar data:
<choice id="100"/><choice id="101"/><choice id="102"/>
However, these values actually translate to other values: red, white, blue respectively. I need to convert that list to the following list:
red,white,blue
As a recap, the "source" table column is varchar, and contains a list of xml attribute values, and those values translate to other values by joining to another table. So the other table has a primary key of id (int) with rows for 100,101,102. Each of those rows has values red,white,blue respectively. I hope this makes enough sense.
Here is the ddl to set up the scenario:
create table datatable(
id int,
data nvarchar(449)
primary key (id)
);
insert into datatable(id, data)
values(1,'<choice id="100"/><choice id="101"/><choice id="102"/>')
,(2,'<choice id="100"/>')
,(3,'<choice id="101"/>')
,(4,'<choice id="102"/>');
create table choicetable(
id int,
choicevalue nvarchar(449)
primary key (id)
);
insert into choicetable(id, choicevalue)
values(100,'red')
,(101,'white')
,(102,'blue');
This would be the first time I've tried parsing XML in this manner so I'm a little stumped where to start. Also, I do not have control over the database I am retrieving the data from (3rd party software).
Without proper sample data it's hard to give an exact query. But you would do something like this
Use CROSS APPLY to convert the varchar to xml
Use .nodes to shred the XML into separate rows.
Join using .value to get the id attribute
Group up, and concatenate using STRING_AGG. You may not need GROUP BY depending on your situation.
SELECT
xt.Id,
STRING_AGG(ot.Value, ',')
FROM XmlTable xt
CROSS APPLY (SELECT CAST(xt.XmlColumn AS xml) ) v(XmlData)
CROSS APPLY v.XmlData.nodes('/choice') x1(choice)
JOIN OtherTable ot ON ot.Id = x1.choice.value('#id','int')
GROUP BY
xt.Id;
I would advise you to store XML data in an xml typed column if at all possible.
I have 4 tables:
Table A:
LogID (unique identifier),
UserID (bigint),
LogDate (date/time),
LogEventID (int),
IPID (varchar(36)),
UserAgentID (varchar(36))
Table B:
IPID (unique identifier),
IPAddress (varchar(255))
Table C:
UserAgentID (unique identifier),
UserAgent (varchar(255))
Table D:
LogEventID (int),
LogEvent (varchar(255))
I am trying to write the to Table A but need to check Table B, Table C and Table D contain data so I can link them. If they don’t contain any data, I would need to create some. Some of the tables may contain data, sometimes none of them may.
Pretty much everything, really struggling
first, you do a insert into table B, C, D WHERE NOT EXISTS
example
INSERT INTO TableB (IPID, IPAddress)
SELECT #IPPD, #IPAddress
WHERE NOT EXISTS
(
SELECT *
FROM TableB x
WHERE x.IPID = #IPID
)
then you insert into table A
INSERT INTO TableA ( . . . )
SELECT . . .
SQL Server doesn't let you modify multiple tables in a single statement, so you cannot do this with a single statement.
What can you do?
You can wrap the multiple statements in a single transaction, if your goal is to modify the database only once.
You can write the multiple statements in stored procedure.
What you really probably want is a view with insert triggers on the view. You can define a view that is the join of the tables with the values from the reference tables. An insert trigger can then check if the values exist and replace them with the appropriate ids. Or, insert into the appropriate table.
The third option does exactly what you want. I find that it is a bit of trouble to maintain triggers, so for an application, I would prefer wrapping the logic in a stored procedure.
I have a local db that I'm trying to insert multiple rows of data, but I do not want duplicates. I do not have a second db that I'm trying to insert from. I have an sql file. The structure is this for the db I'm inserting into:
(db)artists
(table)names-> ID | ArtistName | ArtistURL | Modified
I am trying to do this insertion:
INSERT names (ArtistName, Modified)
VALUES (name1, date),
(name2, date2),
...
(name40, date40)
The question is, how can I insert data and avoid duplication by checking a specific column to this list of data that I want inserted using SQL?
Duplicate what? Duplicate name? Duplicate row? I'll assume no dup ArtistName.
Have UNIQUE(ArtistName) (or PRIMARY KEY) on the table.
Use INSERT IGNORE instead of IGNORE.
(No LEFT JOIN, etc)
I ended up following the advice of #Hart CO a little bit by inserting all my values into a completely new table. Then I used this SQL statement:
SELECT ArtistName
FROM testing_table
WHERE !EXISTS
(SELECT ArtistName FROM names WHERE
testing_table.ArtistName = testing_table.ArtistName)
This gave me all my artist names that were in my data and not in the name table.
I then exported to an sql file and adjusted the INSERT a little bit to insert into the names table with the corresponding data.
INSERT IGNORE INTO `names` (ArtistName) VALUES
*all my values from the exported data*
Where (ArtistName) could have any of the data returned. For example,
(ArtistName, ArtistUrl, Modified). As long as the values returned from the export has 3 values.
This is probably not the most efficient, but it worked for what I was trying to do.
I have 3 tables in SQL server with following fields.
Table 1- id, name, age.
Table 2- id,email, Address.
Table 3- id, name, email.
I wish to use two triggers like, when I insert values on Table 1, id and name should insert in Table 3. When I insert values in Table 2, Email should insert in Table 3 and it should insert at id and name position means it should not show NULL values. Name,id and email should insert in one row.
You should make the View for inserting data from two tables to a single table
You should fix the data model so that it is normalized. You wouldn't need a trigger to replicate data into Table 3 if the name and email columns weren't in 2 tables.
I have 2 tables in 2 different databases. The first table (Custumers) Has many data with 10-12 coulmns.
Then I have the Second table(CustumersNew), it has new columns that should represent the same columns as Custumers just with different names and datatypes. CustumersNew is currently empty. I want to move all of the data from table Custumers to table CustumersNew.
The thing here is that table Custumers UserID column has the datatype uniqueidentifier
and the CustumerNew ID column has the datatype int. So as the rest of the coulmns, they sinply do not match in datatypes.
How do i move the data from A to B?
EDIT:
I'm using MS-SQL
I would use the INSERT INTO CustumersNew(<column list>) SELECT <column list from Custumers CONVERTed to data types that match the data type of corresponding columns in CustumersNew> FROM Custumers statement.
E.g.
INSERT INTO CustumersNew(UserId, Name, Age)
SELECT UserId, CONVERT(NVARCHAR(128), Name), CONVERT(INT, Age)
FROM Custumers
I am assuming that Name and Age are of different types in these two tables. You would need to write a similar convert statements where the data type argument should match the data type in the CustumersNew table.
Since UserId/CustomerId being uniqueidentifier cannot be mapped to integer and I doubt the relevance of the values in this column from a functional perspective, I would model the UserId/CustomerId as an AUTO/IDENTITY column in the new table.
Well, you can't store a uniqueidentifier in an int column so you'll have to come up with a new set of keys.
Most database systems provide a mechanism for sequentially numbering records with integer values. In SQL Server, they use IDENTITY columns, in Oracle they use sequences, I think that in MySql you specify the column as auto_increment.
Once you have set up your new table (with it's auto-numbering scheme) then simply insert the data using SQL:
INSERT INTO CUSTOMERS_NEW (COL2, COL3, COL4)
SELECT COL2, COL3, COL4 FROM CUSTOMERS
Notice that the insert statement does not include the ID column - that should be populated automatically for you.
if you are not able to use insert and have to use update it will be like this
UPDATE change
SET widget_id = (SELECT insert_widget.widget_id
FROM insert_widget
WHERE change.id = insert_widget.id)
WHERE change.id = (SELECT insert_widget.id
FROM insert_widget
WHERE change.id = insert_widget.id)
in this example, I wanted to move widget_id column form insert_widget table to change table, but change table already has data, so I have to use update statement