In SQL, we can create tables either by using the "CREATE TABLE TABLENAME" statement or directly from the SQL server management studio.
In SQL, by using the “Create TABLE” statement, we can create a table in the SQL database. Generally, to create a table in SQL our syntax will be like as shown below.
Following is the syntax of creating a table using the CREATE TABLE statement in SQL Server.
If you observe the above syntax, we defined different types of parameters, those are
Parameter | Description |
---|---|
tablename | It's the name of the table we will create in the database. |
columnname | It is the name of the column that we are going to create. |
datatype | It is useful to specify a type of data we will insert in columns (Ex: int, varchar, datetime, float, etc.). |
size | It means the maximum size of data that we can insert in columns. |
NULL | It will allow users to insert null values in columns. |
NOT NULL | It will not allow the user to insert null values in the column. |
The following is the example of creating a "EmployeeDetails" table with columns empid, empname, designation, salary, location, and joineddate in the database.
Here, empid, salary columns are int datatypes, and it will expect only integer values. The empname, designation, location columns are varchar datatypes, and these will accept a maximum of 50 characters, and the joineddate column is a datetime datatype, it will expect datetime format values.
Once we create a table, try to run the following SQL select query to get the details from the “EmployeeDetails” table.
When we execute the above query, we will get the result below.
You can check this “EmployeeDetails” table in SQL Server management studio under your database section, as shown below.
To create a new table in the database, open SQL Server Management Studio à Open Databases section à Select Required Database to create a table à Right click on Tables section and Select New Table like as shown below.
Whenever we click on “New Table” it will open a new window in that create the table by entering column name, datatype and allow null options like as shown below.
Once we enter all the fields, click the save option available on top or press Ctrl + S to save the table details, whenever we press the save or Ctrl + F5 option, it will ask you for the table name, enter the required table name, and click OK to create a new table in the database.
To check the newly created table, open databases à select your Database à Right-click and select Refresh option to refresh database à Now check it in Tables section that would be like as shown below.