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 ?
Hello Andre, what are you wanting to get with a
CREATE TABLE ... SELECT
in that case? It’s just not to repeat theDLL
columns? I think it ends up being easier to create tables directly if you don’t need the data.– Anthony Accioly
@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.
– Bacco
@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.– PululuK
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
@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 !
– PululuK
@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
@Bacco Thanks bro... We’re in this !!
– PululuK
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 fromINFORMATION_SCHEMA
if you need to automate the process with a script.– Anthony Accioly