Database Reference
In-Depth Information
The basic syntax of the CREATE TABLE command is as follows:
CREATE TABLE table_name (<column_definitions>);
As with creating a database, we can add an IF NOT EXISTS command before the
table name to suppress the error that would appear if the table exists when we try
to create it.
The <column_definitions> part has the following basic pattern:
<column_name> <data_type>
[NOT NULL | NULL]
[DEFAULT <default_value> ]
[AUTO_INCREMENT]
[UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT ' <string> ']
The parts in angle brackets ( <> ) are the bits that we fill in. The parts in square
brackets ( [] ) are optional and the pipe character ( | ) means or . For example, we can
(but do not have to) specify NULL or NOT NULL in a single column definition but we
cannot specify both. Columns are allowed to be NULL , or have no value, by default.
Marking a column as NOT NULL means it can never be empty; some value has to be
assigned to it. Multiple column definitions are separated by commas.
There are many different datatypes ( <data_type> ) to choose from. A datatype
( <data_type> )is a type of data being stored. They exist because each is efficiently
stored somewhat differently. Plain numbers can be treated differently than dates
and vise versa. Common ones include numeric, string, and date datatypes. Numeric
datatypes include INTEGER (commonly written as INT ), and FLOAT (for floating point
numbers). String (or text-based) datatypes include CHAR , TEXT , and VARCHAR . Lastly,
date and time datatypes include DATE , TIME , and DATETIME .
See a complete list of supported datatypes at https://mariadb.com/kb/en/data-
types/ .
Don't worry about trying to memorize all of the different datatypes now.
They'll become second nature as we gain experience using MariaDB.
After specifying the type, length, and precision (for some datatypes), we specify other
options. We can specify whether or not the column is allowed to be empty (or NULL ),
what the default value ( <default_value> ) is, if anything, whether the column auto-
increments (only for numeric datatypes), whether the value in the column should be
UNIQUE , whether the column is a primary key, and a comment, if desired.
 
Search WWH ::




Custom Search