How to create a table (empty) from a selection of other tables?

Asked

Viewed 922 times

1

EXAMPLE:

I have two tables :

  • Tabela1 : two-column id_product, product name;
  • table 2 : three-column id_brand, id_product, tag

I would like to create a Tabela3 from a selection of the Tabela1 and table 2 with only the columns id_product, product name and tag.


CREATE TABLE tabela3 IF NOT EXISTS (
    SELECT p.id_produto, p.nome_produto, m.nome_marca
    FROM tabela1 p
    JOIN tabela2 m 
    ON p.id_produto = m.id_produto
)

Doing it that way I can get almost what I want, only that the Tabela3 is created already filled with table data 1 and 2. What I would like to get is a table made with the structure based on the selection of the other tables.

I tried another way ( using the LIKE):


CREATE TABLE tabela3 IF NOT EXISTS LIKE(
   SELECT p.id_produto, p.nome_produto, m.nome_marca
    FROM tabela1 p
    JOIN tabela2 m 
    ON p.id_produto = m.id_produto
)

I got a nice SQL syntax error.

There’s another way to do it ?

  • 1

    Hello Andre, what are you wanting to get with a CREATE TABLE ... SELECT in that case? It’s just not to repeat the DLL columns? I think it ends up being easier to create tables directly if you don’t need the data.

  • @Anthonyaccioly totally in agreement, I complemented the answer with the syntax of SHOW CREATE TABLE if he chooses this way, that seems to me more sensible.

  • @Anthonyaccioly in fact to Tabela3 will be automatically created as soon as I release my script, so I opted for CREATE TABLE ... SELECT since the creation of the desired table will not be manual. This solution was chosen according to necessity, if not the most viable would be what you suggested.

  • It seems to me an artificial need, because to do the select you already have to know beforehand the structure of qq shape, IE, gives in the same. However, I left the 2 solutions, the clean without being automatic, and the automatic as requested.

  • @Bacco yes you are right, but it will be complicated I have to explain to you all the reasons why I chose this path... Simply put, I’m actually creating a history system just for these two tables and in the specific columns, so I have to copy the structure of these columns as they are... I’ve had negative surprises manually creating the tables !

  • @Andrépka yes, I’ve left you alternatives to see what else is good for your case. I just wanted to reinforce the point, because the automatic can really give you some surprises in cases where the tables have similar field names, etc. Of course, only you really know what you need, so I think it’s cool to expose alternatives, regardless of what I think is best. Test what I proposed, and qq thing leave a comment if you need some adjustment.

  • @Bacco Thanks bro... We’re in this !!

  • 2

    Opá, I meant to say DDL in the above comment. @Bacco has already stressed the point about alternatives and why this is not a good idea. I would like to add another, this syntax becomes quite strange in case you come to need constraints and things like that. As Bacco mentioned, you can use SHOW CREATE TABLE, SHOW COLUMNS and information from INFORMATION_SCHEMA if you need to automate the process with a script.

Show 3 more comments

3 answers

4


To make "automatic":

Create a condition that does not return records:

CREATE TABLE tabela3 (
    SELECT p.id_produto   AS id_produto,
           p.nome_produto AS nome_produto,
           m.nome_marca   AS nome_marca
    FROM tabela1 p, tabela2 m
    WHERE false
);

See working on SQL Fiddle.

  • The WHERE false causes the procedure not to return a row, leaving the new table empty.

  • Do not forget to use ALIAS in cases where there may be ambiguity of names, to avoid problems.

  • Since you are not using the data, you can simplify JOIN this way:

    FROM tabela1 p, tabela2 m
    

    (it has no problem to keep as is the original too, both work)


If you want to have more control over the procedure

You can execute this command in the tables, and recover the column types to redo the CREATE manually, almost always is preferable:

SHOW CREATE TABLE meubanco.minhatabela;

Then you copy the column settings you want, in the order that is best for your new table, and adjust everything you need.

0

With TOP 0 you will only have the definition of the columns, without their values.

SELECT TOP 0 p.id_produto, p.nome_produto, m.nome_marca INTO tabela3 
FROM tabela1 p
INNER JOIN tabela2 m 
ON p.id_produto = m.id_produto

If you want to check if the table exists enter the following conditional

IF OBJECT_ID('tabela3') IS NULL
BEGIN
   SELECT TOP 0 p.id_produto, p.nome_produto, m.nome_marca INTO tabela3 
   FROM tabela1 p
   INNER JOIN tabela2 m 
   ON p.id_produto = m.id_produto
END
  • 1

    The idea is good, but TOP and OBJECT_ID do not work in Mysql

0

Hello! Well, I know the code is bad. But in a quick test here, it has fulfilled what you’re looking for. See, if the number of fields is greater than the current three, then it is better to refactor. Hug!

CREATE TABLE tabela3 (
 SELECT p.id_produto, p.nome_produto, m.nome_marca
 FROM tabela1 p
 JOIN tabela2 m 
 ON p.id_produto = m.id_produto
 WHERE p.id_produto IS NULL 
  AND p.nome_produto IS NULL
  AND m.nome_marca IS NULL
);

But see the second way, really correct informed by our friend Ls_dev in the comments, using (WHERE FALSE)

CREATE TABLE tabela3 (
SELECT p.id_produto, p.nome_produto, m.nome_marca
FROM tabela1 p
JOIN tabela2 m 
ON p.id_produto = m.id_produto
WHERE FALSE);
  • Two to go IS NULL. Anyway, the SELECT will return lines in which the condition checks. It is not guaranteed that these lines do not exist!

  • Hello Ls_dev, thanks for the info. But I just tested with the tables filled and rolled.

  • The IS NULL condition can be set last, in this case after separating each field with the condition AND. Anyway, thank you for the remark.

  • Test with INSERT INTO tabela1 VALUES(1, 1); INSERT INTO tabela2 VALUES(1, NULL);. Table 3 will not be created empty. This solution will work if you do WHERE FALSE;

  • I’ll test it here

  • Ls_dev, I did the test here and the third table (Tabela3) is being assembled with the 3 fields and no tuple. According to what our colleague is looking for. No mistake.

  • Mysql returned an empty set (e.g. zero records). (Query took 0.0003 seconds.) id_product product product name_brand

  • Server type: Mariadb Server version: 10.1.18-Mariadb-1~jessie - mariadb.org Binary Distribution

  • 1

    Sorry to insist, but that’s not correct. http://rextester.com/RWGNY81139 IS NULL is only applied to nome_marca. Anyway, the right thing would be to eliminate the possibility of the condition being true at all, hence WHERE FALSE.

  • ls_dev, you’re right. I’ll edit the answer. Thank you!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.