当前位置:Gxlcms > mysql > mysql创建表

mysql创建表

时间:2021-07-01 10:21:17 帮助过:98人阅读

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

temporary创建临时表

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

原始表数据:

  1. mysql> select * from student;
  2. +--------+----------+---------+-----------+
  3. | stu_id | stu_name | stu_tel | stu_score |
  4. +--------+----------+---------+-----------+
  5. | 1 | a | 151 | 60 |
  6. | 2 | b | 152 | 61 |
  7. | 3 | c | 153 | 62 |
  8. | 4 | d | 154 | 63 |
  9. +--------+----------+---------+-----------+
  10. 4 rows in set (0.00 sec)
创建临时表:

  1. mysql> create temporary table student(
  2. -> stu_id int,
  3. -> stu_name varchar(5),
  4. -> stu_tel int(5),
  5. -> stu_score int(2)
  6. -> );
  7. Query OK, 0 rows affected (0.14 sec)

查询表student:

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

if not esists 判断表存在

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

  1. create table if not exists student(
  2. stu_id int,
  3. stu_name varchar(5),
  4. stu_tel int(5),
  5. stu_score int(2)
  6. );
  7. drop table if exists student;

复制表

  1. create table student1 like student;

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

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

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

其他参数:

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

例:

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

人气教程排行