Which statement is correct regarding the following SQL statement:

CREATE TABLE persons AS SELECT name, surname FROM person;
Explanation
CREATE TABLE ... AS SELECT ... query allows to create a new table based on the structure and data from other tables (based on the SELECT statement). In general case, all the data returned by SELECT statement will be copied into a new table. This statement doesn't create any indexes automatically. If you want to create them, thought, - you should specify it before the SELECT statement: CREATE TABLE new_table (UNIQUE (col_1)) SELECT col_1, col_2 FROM old_table;
Theory
  • SQL - CREATE Table

    Creating a basic table involves naming the table and defining its columns and each column's data type.
    The SQL CREATE TABLE statement is used to create a new table.

    Syntax

    CREATE TABLE table_name(
       column1 datatype,
       column2 datatype,
       .....
       columnN datatype,
       PRIMARY KEY( one or more columns )
    );
    
    CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.
    Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer with an example below.
    A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement.
    Read more.
  • CREATE Table Example

    Following is an example, which creates a CUSTOMERS table with ID as primary key and NOT NULL are the constraints showing that these fields can not be NULL while creating records in this table:
    SQL> CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) , 
       PRIMARY KEY (ID)
    );
    
    You can verify if your table has been created successfully by using DESC command as follows:
    
    SQL> DESC CUSTOMERS;
    Field   | Type          | Null | Key | Default | Extra
    --------+---------------+------+-----+---------+------
    ID      | int(11)       | NO   | PRI |         |      
    NAME    | varchar(20)   | NO   |     |         |      
    AGE     | int(11)       | NO   |     |         |      
    ADDRESS | char(25)      | YES  |     | NULL    |      
    

    Read more.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes