Making new table for combining

It is quite normal to maintain multiple tables within a database, each holding a different type of information. For example, consider the case of a customers table that needs to be able to be cross-referenced with publications purchased from the classics table.
Next example to create this new table and populate it with
three customers and their purchases.

Creating and populating the customers table:

CREATE TABLE customers (
name VARCHAR(128),
isbn VARCHAR(13),
PRIMARY KEY (isbn)) ENGINE InnoDB;


INSERT INTO customers(name,isbn)
VALUES(‘Mikko Mekaanikko’,’5051011157622′);
INSERT INTO customers(name,isbn)
VALUES(‘Tony Asentaja’,’5052498183227′);
INSERT INTO customers(name,isbn)
VALUES(‘Katto Kassinen’,’6417832081437′);

SELECT * FROM customers;

There’s also a shortcut for inserting multiple rows of data, you can replace the three separate INSERT INTO queries with a single one listing the data to be inserted, separated by commas, like this 🙂

INSERT INTO customers(name,isbn) VALUES
(‘Mikko Mekaanikko’,’ 5051011157622 ‘),
(‘Tony Asentaja’,’ 5052498183227 ‘),
(‘Katto Kassinen’,’ 6417832081437 ‘);

Of course, in a proper table containing customers’ details there would also be addresses, phone numbers, email addresses, and so on, but they aren’t necessary for this explanation. While creating the new table, you should have noticed that it has something in common with the classics table: a column called isbn. Because it has the same meaning in both tables (an ISBN refers to a book or product key in CD:s, and always the same book/product), we can use this column to tie the two tables together into a single query:

SELECT name,author,title FROM customers,classics
WHERE customers.isbn=classics.isbn;

Leave a Reply

Your email address will not be published. Required fields are marked *