We have seen how to create a database in the earlier post. You can refer it by clicking the following link.
In this post I am going to show you how to create a table in a specific database. Let us have a glance at this.
We list out all the existing databases:
From the above list of databases I would like to use ‘sports’ database. For this we use the following syntax.
Now we are in sports database. To check whether the database ‘sports’ is having any tables or not.
The sports database is not having any tables so now we are going to create table. For this we use the following syntax:
|CREATE TABLE table_name (column_name column_type…);|
Here we are creating a table named ‘Players’ in database ‘sports’;
We can check whether the table Players is created in the database ‘sports’ or not.
We can see the table structure as shown in below:
Here it takes Null value as default because we haven’t mentioned in query while creating. If you don’t want to keep this field with NULL value then we must mention as NOT NULL in the create query. If you try to create a table with the same name you will be displayed an error. Hence I used table name as ‘players_1’.
Here the fields name and sport do not accept any null values as we mentioned them as not null in create query.
If we try to create a table by using if not exists, then you will be displayed a warning and is shown in below:
To watch this tutorial post on YouTube Channel