Database Reference
In-Depth Information
A primary key is a column or group of columns, which uniquely identifies a specific
row in the table. No other row in a given table is allowed to have the same primary
key. If we try to input a row with a primary key that matches another primary key in
the table we will get an error.
For our preceding
employees
example, we might use the following
CREATE
statement to create the table (use the
test
database or
CREATE
a new database
and then
USE
it if you want to follow along):
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
surname VARCHAR(100),
givenname VARCHAR(100),
pref_name VARCHAR(50),
birthday DATE
);
When we run the preceding code, the output looks like this:
Query OK, 0 rows affected (0.00 sec)
A result of
Query OK
means that the table was created successfully. Zero rows
were affected because this brand new table has no data in it yet.
Full documentation of the
CREATE TABLE
command can be found at
https://mariadb.com/kb/en/create-table/
.
Showing the command used to create a table
At any time, for example, if we want to create a similar table in a different database,
we can use the
SHOW CREATE TABLE
command to show a command that will recreate
the table exactly. Here is an example:
MariaDB [test]> SHOW CREATE TABLE employees\G
*************************** 1. row ***************************
Table: employees
Create Table: CREATE TABLE `employees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`surname` varchar(100) DEFAULT NULL,
`givenname` varchar(100) DEFAULT NULL,
`pref_name` varchar(50) DEFAULT NULL,
`birthday` date DEFAULT NULL,