6

I would like to know how MySQL interprets the CREATE TABLE syntax:

If I write:

CREATE TABLE tbl1 (
    `v1` int,
    `v2` int
     CONSTRAINT idx PRIMARY KEY (v1)
)
SELECT a, b FROM tbl2;
  • Does it determine which value goes into v1 and which goes into v2 by their order in the select statement?

  • Does it use the names I designate in the CREATE TABLE statement or does it take them from the select statement?

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

1
  • I sugest that you use PhpMyAdmin. It is much easier!!! Commented Aug 15, 2012 at 2:33

1 Answer 1

12

With your current solution you'll get a table with the columns v1, v2, a, and b.

To see how it is done properly, see the "CREATE TABLE ... SELECT Statement" chapter in the official MySQL documentation.

So if you just want v1 and v2 with an index on v1, do it like this:

CREATE TABLE tbl1 (PRIMARY KEY (v1))
SELECT a v1,
       b v2
FROM tbl2;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.