mysql创建表

php中文网
发布: 2016-06-07 15:46:57
原创
1715人浏览过

temporary创建临时表 使用关键字temporary可以创建临时表。临时表只能对创建它的用户课件。临时表可以和持久表有同样的表明,此时临时表会隐藏持久表。 例如: 原始表数据: mysql select * from student;+--------+----------+---------+-----------+| stu_

temporary创建临时表

使用关键字temporary可以创建临时表。临时表只能对创建它的用户课件。临时表可以和持久表有同样的表明,此时临时表会隐藏持久表。
例如:

原始表数据:

mysql> select * from student;
+--------+----------+---------+-----------+
| stu_id | stu_name | stu_tel | stu_score |
+--------+----------+---------+-----------+
|      1 | a        |     151 |        60 |
|      2 | b        |     152 |        61 |
|      3 | c        |     153 |        62 |
|      4 | d        |     154 |        63 |
+--------+----------+---------+-----------+
4 rows in set (0.00 sec)
登录后复制
创建临时表:
mysql> create temporary table student(
    -> stu_id          int,
    -> stu_name        varchar(5),
    -> stu_tel         int(5),
    -> stu_score       int(2)
    -> );
Query OK, 0 rows affected (0.14 sec)
登录后复制

查询表student:

mysql> select * from student;
Empty set (0.00 sec)
登录后复制
没有任何数据,是因为临时表隐藏了永久表。

if not esists 判断表存在

如果表已经存在时不能再创建永久表。可通过if not exists 判断是否存在
也可以通过if exists 判断并删除一个表。

create table if not exists  student(
stu_id          int,
stu_name        varchar(5),
stu_tel         int(5),
stu_score       int(2)
);
drop table if exists student;
登录后复制

复制表

create table student1 like student;
登录后复制

复制表结构的同时复制数据,并更改列名

Reclaim.ai
Reclaim.ai

为优先事项创建完美的时间表

Reclaim.ai 152
查看详情 Reclaim.ai
mysql> create table student1 as
    -> (select stu_id as id,stu_name as name,stu_tel as tel,stu_score as score 
    -> from student);
Query OK, 4 rows affected (0.22 sec)
Records: 4  Duplicates: 0  Warnings: 0
登录后复制
mysql> select * from student1;
+----+------+------+-------+
| id | name | tel  | score |
+----+------+------+-------+
|  1 | a    |  151 |    60 |
|  2 | b    |  152 |    61 |
|  3 | c    |  153 |    62 |
|  4 | d    |  154 |    63 |
+----+------+------+-------+
4 rows in set (0.00 sec)
登录后复制
复制表时可以给新表设置新的约束
例如将新表的id设置为主键。

mysql> create table student1(stu_id int primary key) as (select * from student);
Query OK, 4 rows affected (0.22 sec)
Records: 4  Duplicates: 0  Warnings: 0
登录后复制
列名表中没有说明的行默认使用原表中的约束。

其他参数:

auto_increment为新添加的行自动递增的分配一个id,也可设置递增分配id的起始id
default 为列设定默认值。
comment 为列添加说明

例:

create table student(
stu_id          int primary key auto_increment comment '学生ID',
stu_name        varchar(5) comment '学生姓名',
stu_tel         int(5) comment '学生电话',
stu_score       int(2) default 60 comment '学生成绩'
)auto_increment = 100;
登录后复制
mysql> insert into student values(NULL,'a',150,default);
Query OK, 1 row affected (0.03 sec)
登录后复制
mysql> select * from student;
+--------+----------+---------+-----------+
| stu_id | stu_name | stu_tel | stu_score |
+--------+----------+---------+-----------+
|    100 | a        |     150 |        60 |
+--------+----------+---------+-----------+
1 row in set (0.00 sec)
登录后复制
mysql> select column_name,column_comment
    -> from information_schema.columns
    -> where table_name = 'student';
+-------------+----------------+
| column_name | column_comment |
+-------------+----------------+
| stu_id      | 学生ID         |
| stu_name    | 学生姓名       |
| stu_tel     | 学生电话       |
| stu_score   | 学生成绩       |
+-------------+----------------+
4 rows in set, 0 warnings (0.33 sec)
登录后复制

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号