- MariaDB - Useful Functions
- MariaDB - Backup Loading Methods
- MariaDB - Backup Methods
- MariaDB - SQL Injection Protection
- MariaDB - Managing Duplicates
- MariaDB - Sequences
- MariaDB - Table Cloning
- MariaDB - Temporary Tables
- Indexes & Statistics Tables
- MariaDB - Alter Command
- MariaDB - Transactions
- MariaDB - Regular Expression
- MariaDB - Null Values
- MariaDB - Join
- MariaDB - Order By Clause
- MariaDB - Like Clause
- MariaDB - Delete Query
- MariaDB - Update Query
- MariaDB - Where Clause
- MariaDB - Select Query
- MariaDB - Insert Query
- MariaDB - Drop Tables
- MariaDB - Create Tables
- MariaDB - Data Types
- MariaDB - Select Database
- MariaDB - Drop Database
- MariaDB - Create Database
- MariaDB - Connection
- MariaDB - PHP Syntax
- MariaDB - Administration
- MariaDB - Installation
- MariaDB - Introduction
- MariaDB - Home
MariaDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MariaDB - Create Tables
In this chapter, we will learn how to create tables. Before creating a table, first determine its name, field names, and field definitions.
Following is the general syntax for table creation −
CREATE TABLE table_name (column_name column_type);
Review the command appped to creating a table in the PRODUCTS database −
databaseproducts_ tbl( product_id INT NOT NULL AUTO_INCREMENT, product_name VARCHAR(100) NOT NULL, product_manufacturer VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( product_id ) );
The above example uses “NOT NULL” as a field attribute to avoid errors caused by a null value. The attribute “AUTO_INCREMENT” instructs MariaDB to add the next available value to the ID field. The keyword primary key defines a column as the primary key. Multiple columns separated by commas can define a primary key.
The two main methods for creating tables are using the command prompt and a PHP script.
The Command Prompt
Utipze the CREATE TABLE command to perform the task as shown below −
root@host# mysql -u root -p Enter password:******* mysql> use PRODUCTS; Database changed mysql> CREATE TABLE products_tbl( -> product_id INT NOT NULL AUTO_INCREMENT, -> product_name VARCHAR(100) NOT NULL, -> product_manufacturer VARCHAR(40) NOT NULL, -> submission_date DATE, -> PRIMARY KEY ( product_id ) -> ); mysql> SHOW TABLES; +------------------------+ | PRODUCTS | +------------------------+ | products_tbl | +------------------------+
Ensure all commands are terminated with a semicolon.
PHP Create Table Script
PHP provides mysql_query() for table creation. Its second argument contains the necessary SQL command −
<html> <head> <title>Create a MariaDB Table</title> </head> <body> <?php $dbhost = localhost:3036 ; $dbuser = root ; $dbpass = rootpassword ; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ){ die( Could not connect: . mysql_error()); } echo Connected successfully<br /> ; $sql = "CREATE TABLE products_tbl( ". "product_id INT NOT NULL AUTO_INCREMENT, ". "product_name VARCHAR(100) NOT NULL, ". "product_manufacturer VARCHAR(40) NOT NULL, ". "submission_date DATE, ". "PRIMARY KEY ( product_id )); "; mysql_select_db( PRODUCTS ); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die( Could not create table: . mysql_error()); } echo "Table created successfully "; mysql_close($conn); ?> </body> </html>
On successful table creation, you will see the following output −
mysql> Table created successfullyAdvertisements