CREATE TABLE
After creating a database in MariaDB, one can create a new table in the selected database using the MariaDB CREATE TABLE statement.
Syntax 1: To create a Table in MariaDB.
CREATE TABLE table_name ( column_name data_type column_constraint );
Syntax 2: To check all the existing Tables in a database.
SHOW tables;
Example 1: Creating a table with NULL and NOT NULL column constraint.
CREATE TABLE players ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, sports VARCHAR(50) ); |
Explanation:
In the above example the “id”, “name”, “sports” represents the name of the columns. INT and VARCHAR are data types and NULL and NOT NULL defines the column constraint, where NULL means acceptance of NULL values in that column and NOT NULL means no acceptance of NULL values in that column.
Example 2: Creating a table with a PRIMARY KEY column constraint.
CREATE TABLE players ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, sports VARCHAR(50), PRIMARY KEY(id) ); |
Explanation:
In the above example the “id”, “name”, “sports” represents the name of the columns. INT and VARCHAR are data types and NULL and NOT NULL defines the column constraint, where NULL means acceptance of NULL values in that column and NOT NULL means no acceptance of NULL values in that column. Here, “id” is defined as the Primary Key Column. The primary key column is used for distinguishing a unique row in a table.
Create table using Management Studio:
To create a table using management studio tool, select the database and right-click on that.
- Click on “Create New” from the dropdown menu.
- Click on “Table” from the generated dropdown menu.
Now you can add a name to the table along with the various other options available.