千家信息网

Mysql怎么创建数据表

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"Mysql怎么创建数据表",在日常操作中,相信很多人在Mysql怎么创建数据表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Mysql怎么创建数据表"
千家信息网最后更新 2026年11月02日Mysql怎么创建数据表

这篇文章主要介绍"Mysql怎么创建数据表",在日常操作中,相信很多人在Mysql怎么创建数据表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Mysql怎么创建数据表"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

C:\Users\admin>mysql -h localhost -u root -pmysql

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mydata |

| mysql |

| performance_schema |

| sys |

| test |

+--------------------+

6 rows in set (0.00 sec)

mysql> use mydata

Database changed

mysql> create table mydata1(

-> id int,

-> name varchar(20),

-> sex boolean

-> );

Query OK, 0 rows affected (0.36 sec)

mysql> desc mydata1;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(20) | YES | | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.02 sec)

mysql> show tables;

+------------------+

| Tables_in_mydata |

+------------------+

| mydata1 |

+------------------+

1 row in set (0.00 sec)

5.1完整性约束条件

Primary key

主键,标识唯一

Foreign key

标识该属性为该表的外键,联系表的主键

Not null

属性不能为空

Unique

属性的值是唯一的

Auto_increment

值自动增加,mysql的sql语句的特色

Default

列设置默认值

5.2 主键

单字段主键和多字段主键

mysql> create table mydata2(

-> id int primary key, #单一字段主键

-> name varchar(20),

-> sex boolean);

Query OK, 0 rows affected (0.23 sec)

mysql> show tables;

+------------------+

| Tables_in_mydata |

+------------------+

| mydata1 |

| mydata2 |

+------------------+

2 rows in set (0.00 sec)

mysql> desc mydata2;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(20) | YES | | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> alter table mydata2 drop primary key;

Query OK, 0 rows affected (0.68 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc mydata2;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | | NULL | |

| name | varchar(20) | YES | | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> alter table mydata2 add primary key(id,name); #设置多字段主键

Query OK, 0 rows affected (0.49 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc mydata2;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(20) | NO | PRI | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

也可以在create table 定义中定义primary key

mysql> create table mydata3(

-> id int,

-> name varchar(20),

-> sex boolean,

-> primary key(id,name)

-> );

Query OK, 0 rows affected (0.24 sec)

mysql> desc mydata3;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(20) | NO | PRI | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

5.3 外键 foreign key

mysql> create table mydata4(

-> id int primary key,

-> name varchar(30),

-> sex boolean,

-> constraint my_fk foreign key(id) references mydata3(id)

-> );

Query OK, 0 rows affected (0.26 sec)

mysql> desc mydata4;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(30) | YES | | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

5.4 not null 非空

mysql> create table mydata5(

-> id int primary key,

-> name varchar(20) not null);

Query OK, 0 rows affected (0.28 sec)

mysql> desc mydata5;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(20) | NO | | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

5.5 unique 唯一性

mysql> create table mydata6(

-> id int primary key,

-> name varchar(20) unique);

Query OK, 0 rows affected (0.35 sec)

mysql> desc mydata6;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

| name | varchar(20) | YES | UNI | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

5.6 auto_increment

必须为主键的一部分

mysql> create table mydata7(

-> id int primary key auto_increment,

-> name varchar(20))

-> ;

Query OK, 0 rows affected (0.24 sec)

mysql> desc mydata7;

+-------+-------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+----------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| name | varchar(20) | YES | | NULL | |

+-------+-------------+------+-----+---------+----------------+

2 rows in set (0.00 sec)

5.7 默认值

mysql> create table mydata8(

-> id int primary key auto_increment,

-> name varchar(20) unique,

-> address varchar(100) not null,

-> city varchar(20) default 'suzhou',

-> socre float default 0);

Query OK, 0 rows affected (0.35 sec)

mysql> desc mydata8;

+---------+--------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+---------+--------------+------+-----+---------+----------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| name | varchar(20) | YES | UNI | NULL | |

| address | varchar(100) | NO | | NULL | |

| city | varchar(20) | YES | | suzhou | |

| socre | float | YES | | 0 | |

+---------+--------------+------+-----+---------+----------------+

5 rows in set (0.04 sec)

5.8 查看表结构

mysql> show create table mydata1 \G;

*************************** 1. row ***************************

Table: mydata1

Create Table: CREATE TABLE `mydata1` (

`id` int(11) DEFAULT NULL,

`name` varchar(20) DEFAULT NULL,

`sex` tinyint(1) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> desc mydata1;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(20) | YES | | NULL | |

| sex | tinyint(1) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

5.9 修改表结构

mysql> alter table mydata1 rename to mydata; #修改表名

Query OK, 0 rows affected (0.23 sec)

mysql> alter table mydata1 modify sex varchar(1); #修改列属性

Query OK, 0 rows affected (0.77 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 change city address varchar(20);

mysql> alter table mydata1 change sex city int; #修改列名和属性

Query OK, 0 rows affected (0.94 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 add city int; #添加列名

Query OK, 0 rows affected (0.53 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 add sal int after address; #在address栏位后面加列

Query OK, 0 rows affected (0.35 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 add uid int first; #加列为首列

Query OK, 0 rows affected (0.45 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 drop city; #删除列

Query OK, 0 rows affected (0.50 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 modify sal int after name; #修改列的位置

Query OK, 0 rows affected (0.53 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table mydata1 modify id int first; #修改为首列

Query OK, 0 rows affected (0.54 sec)

Records: 0 Duplicates: 0 Warnings: 0

CHANGE 对列进行重命名或更改列的类型,需给定旧的列名称和新的列名称、当前的类型 MODIFY 可以改变列的类型,此时不需要重命名(不需给定新的列名称)

mysql> alter table mydata1 engine=myisam; #修改表的存储引擎

Query OK, 0 rows affected (1.47 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> drop table mydata8; #删除表

Query OK, 0 rows affected (0.22 sec)

到此,关于"Mysql怎么创建数据表"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

属性 数据 数据表 字段 学习 名称 类型 更多 标识 结构 帮助 实用 接下来 位置 唯一性 完整性 引擎 文章 方法 条件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 耶加雪菲 互联网的科技创新 壹账通互联网科技 天津摩尔互联网科技 汇久创客互联网科技 易通互联网络科技有限公司 菲比寻常互联网科技 中国最先进互联网科技公司 济南和联互联网科技有限公司 玖二柒互联网科技公司待遇 随着互联网科技发展 央行互联网金融科技公司 顶级互联网科技成果有哪些 南山科技园互联网公司有哪些 世界互联网科技公司市值排名 四川慕妮娅互联网科技有限公司 启辰互联网科技 广州燕处互联网科技有限公司 互联网科技大会大佬 山东亿川互联网科技有限责任公司 北京互联网科技公司 北京互联网科技有限公司 合肥互联网科技公司 世界互联网领先科技成果 深圳卡尔迅互联网科技有限公司怎么样 互联网科技有哪些公司 赶超互联网科技 微信多开 乌鲁木齐新华智原互联网科技学校 半导体是互联网高科技吗 东营紫格互联网科技
0