Marcius Leandro Marcius Leandro - 6 months ago 63
MySQL Question

SQL - Generate automatic insertion from a second table

I have a table with more than 7000 book inserted, however I need to create another table for Gibis and Files, to have control of all the inserts I have a generic table called collection.

It follows the structures:

Collection:

CREATE TABLE collection(
collectionCod INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
collectionType VARCHAR(50) NOT NULL,
collectionTitle VARCHAR(100),
collectionImg VARCHAR(100),
collectionInfo INT)


Book:

CREATE TABLE book(
bookInfo INT PRIMARY KEY,
bookSubject VARCHAR(100),
bookTitle VARCHAR(100),
bookAuthor VARCHAR(100))


I need to generate an insertion in the collection for each existing book, as I do in SQL? Putting the type as book.

Answer Source

Something like this:

insert into collection (collectionTipy, collectionTitle, collectionImg, collectionInfo)
    select bookSubject, bookTitle, NULL, bookAuthor
    from book;

I don't know what the exact mapping is between the fields in the book and collection, but this is the idea.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download