MySQL 可以使用 CREATE TABLE SQL 语句创建表

创建 MySQL 数据表需要 表名 、表字段名 、定义每个表字段

CREATE TABLE 语句语法

下面的 SQL 语句为创建 MySQL 数据表的通用语法

CREATE TABLE table_name (column_name column_type);

例如下面的 SQL 语句将在 twle 数据库中创建 tbl_language 表

CREATE TABLE IF NOT EXISTS `tbl_language`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`url` VARCHAR(128) NOT NULL,
`founded_at` DATE,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

如果不想字段为 NULL 可以设置字段的属性为 NOT NULL

往表中插入数据时,如果输入该字段的数据为 NULL ,就会报错

AUTO_INCREMENT 定义列为自增的属性,一般用于主键,数值会自动加 1

PRIMARY KEY 关键字用于定义列为主键

可以使用多列来定义主键,列间以逗号分隔

ENGINE 设置存储引擎

CHARSET 设置编码

通过命令提示符创建表

可以在 mysql> 命令行窗口中使用 SQL 语句 CREATE TABLE 来创建数据表

下面的操作在 twle 数据库中创建 languages 表

以下为创建数据表 简单教程_tbl 实例:

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.2.13-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use twle;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [twle]> CREATE TABLE IF NOT EXISTS `tbl_language`(
-> `id` INT UNSIGNED AUTO_INCREMENT,
-> `name` VARCHAR(64) NOT NULL,
-> `url` VARCHAR(128) NOT NULL,
-> `founded_at` DATE,
-> PRIMARY KEY ( `id` )
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.03 sec)
MariaDB [twle]> show tables;
+----------------+
| Tables_in_twle |
+----------------+
| customer |
| sites |
| tbl_language |
+----------------+
3 rows in set (0.00 sec)
MariaDB [twle]> DESC tbl_language;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| url | varchar(128) | NO | | NULL | |
| founded_at | date | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)

使用箭头标记 -> 不是 SQL 语句的一部分,它仅仅表示一个新行,如果一条SQL语句太长,可以通过回车键来创建一个新行来编写 SQL 语句

MySQL 命令终止符为分号 (;)

执行 CREATE TABLE 命令后可以使用 SHOW TABLES 命令来查看当前数据库中有哪些表

然后可以使用 DESC [表名] 来查看某个表的数据结构

PHP 创建数据表

PHP 使用 PDO::exec() 函数来创建或者删除 MySQL 表

PDO::exec() 函数只有一个参数,就是想要执行的 SQL 语句

PDO::exec 语法格式

int PDO::exec ( string $statement )

返回受修改或删除 SQL 语句影响的行数。如果没有受影响的行,则返回 0

参数

参数

说明

statement

要被预处理和执行的 SQL 语句,查询中的数据应该被妥善地转义

范例

下面的 PHP 代码演示了如何用 PDO_MySQL 在数据库 twle 中创建 tbl_language 表

/*
* filename: main.php
* author: 简单教程(www.twle.cn)
*
* Copyright © 2015-2065 www.twle.cn. All rights reserved.
*/
$sql=<<
CREATE TABLE IF NOT EXISTS `tbl_language`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`url` VARCHAR(128) NOT NULL,
`founded_at` DATE,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
EOF;
try {
$dbh = new PDO('mysql:host=127.0.0.1;dbname=twle', 'root', '');
$rs = $dbh->exec($sql);
echo "在 twle 数据库中成功创建表 tbl_language\n";
}
catch (PDOException $e)
{
echo "错误!: " , $e->getMessage() , "\n";
}

运行以上 PHP 代码,输出结果如下

$ php main.php

在 twle 数据库中成功创建表 tbl_language

注意: 不管创建成功与否,PDO::exec() 函数都是返回 0 ,所以无法从结果值里判断是否创建成功

我们可以使用 mysql 客户端命令查看 twle 里有没有 tbl_language 表

MariaDB [twle]> USE twle;
Database changed
MariaDB [twle]> SHOW TABLES;
+----------------+
| Tables_in_twle |
+----------------+
| customer |
| sites |
| tbl_language |
+----------------+
3 rows in set (0.00 sec)