SQL Essentials Training & Certification
- 9k Enrolled Learners
- Self Paced
SQL or Structured Query Language consists of various commands to handle relational databases. These commands are segregated into various categories such as DDL, DML, DCL, and TCL. One important query out of the lot is the CREATE Table query from the DDL commands. So, in this article on Create Table in SQL, you will learn about CREATE TABLE statement in the following sequence:
The create table statement is used to create a table for the database you are using. This table can have n rows and m columns based on the requirement. So, with the help of this query, you can basically store data in the form of rows and columns.
Next, in this article on create table in SQL, let us see the syntax of Create statement.
The syntax of the CREATE TABLE statement is as follows:
CREATE TABLE tablename ( column1 data type, column2 data type, column3 data type, column4 data type, .... columnN data type);
Here, the column parameters represent the name of the columns to be included in the table. Similarly, the data type parameter represents the type of data columns can store. Example: character, integer, date, varchar, etc.
CREATE TABLE students ( studentID int, studentname varchar(255), parentname varchar(255), address varchar(255), phonenumber int );
Now, once you create the tables, you can go forward and insert values into the table by using the Insert query. But, what if you had to create a table using another existing table? How will you do that?
So, next, in this article on create table in SQL, let us look into the same.
To create another table from an existing table, you have to use the following syntax:
CREATE TABLE newtablename AS SELECT column1, column2,..., columnN FROM existingtablename WHERE ....;
Here, you are trying to create a new table from an existing one. Also, you are choosing the required columns from the existing table, based on a condition. But, mentioning a condition is not mandatory.
CREATE TABLE sampletable AS SELECT studentID, studentname FROM students;
Note: The new table gets the same column definitions as that of the old one. Also, if your existing table has any values stored, then automatically the new table will be filled with those values.
With this, we come to an end to this article. I hope you understood, how to use the CREATE TABLE in SQL. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.
Got a question for us? Please mention it in the comments section of this article and I will get back to you.