Create table using SELECT syntax in MySQL
P粉715228019
P粉715228019 2023-07-20 22:47:01
0
1
586

I want to know how MySQL interprets the CREATE TABLE syntax:

If it were written like this:

CREATE TABLE tbl1 (
    `v1` int,
    `v2` int
     CONSTRAINT idx PRIMARY KEY (v1)
)
SELECT a, b FROM tbl2;
  • Does it decide which values ​​go into v1 and which values ​​go into v2 based on the order in the select statement?

  • Does it use the name I specified in the CREATE TABLE statement, or does it get the name from the select statement?

I have used CREATE TABLE XX SELECT val FROM YY before, but would like to be more specific about the above syntax.

P粉715228019
P粉715228019

reply all(1)
P粉741678385

Based on the current solution, you will get a table with columns v1 v2 a and b.

To learn how to do it correctly, see the "CREATE TABLE ... SELECT Statement" chapter, please refer to the MySQL official documentation. .

So if you just want v1 and v2 to have an index on v1, like this:

CREATE TABLE tbl1 (PRIMARY KEY (v1))
SELECT a v1,
       b v2
FROM tbl2;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template