MySQL数据表

一、USE命令打开数据库,创建数据表;

打开数据库:

mysql> USE t2;

 创建数据表:

CREATE TABLE [IF NOT EXISTS] table_name (

          column_name data_type,

          ......)

查看当前数据库:

mysql> SELECT DATABASE();

查看数据库中的所有表:

mysql> SHOW TABLES FROM mysql;

查看数据表结构:

mysql> DESCRIBE tb2;

 

mysql> SHOW COLUMNS FROM tb2;

实例: 

D:\MySQL\mysql-8.0.13-winx64\bin>mysql -uroot -pFigh.1234
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 12
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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 |
| mysql              |
| performance_schema |
| sys                |
| t2                 |
+--------------------+
5 rows in set (0.03 sec)

mysql> USE t2;
Database changed

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| t2         |
+------------+
1 row in set (0.00 sec)

mysql> CREATE TABLE tb2(
    -> username VARCHAR(20),age TINYINT UNSIGNED,
    -> salary FLOAT(8,2));
Query OK, 0 rows affected (0.11 sec)

mysql> SHOW TABLES;
+--------------+
| Tables_in_t2 |
+--------------+
| tb1          |
| tb2          |
+--------------+
2 rows in set (0.00 sec)

mysql> DESCRIBE tb2;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2)          | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM tb2;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2)          | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>

二、插入记录

INSERT [INTO] tb_name [(col_name,....)] VALUES(val,....);如果某一列有主键约束,传入null/default,则会遵循原有规则;如果某一列有默认值,传入default,则会遵从默认值;

insert tb_name set col_name=col_val,col_name=col_val2...;此方法可以使用子查询;

 

查找记录:selcet exp... from tb_name;

mysql> describe tb2;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2)          | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert tb2 values('tom',111);
ERROR 1136 (21S01): Column count doesn't match value count at row 1

mysql> insert tb2 values('Rac',39,14932.43);
Query OK, 1 row affected (0.07 sec)

mysql> insert tb2(username,age) values("JONE",32);
Query OK, 1 row affected (0.08 sec)

mysql> select * from tb2;
+----------+------+----------+
| username | age  | salary   |
+----------+------+----------+
| Rac      |   39 | 14932.43 |
| JONE     |   32 |     NULL |
+----------+------+----------+
2 rows in set (0.00 sec)

mysql>

NULL:允许插入null字段;

NOT NULL :不允许插入null字段;

三、自动编号:AUTO_INCREMENT

必须与主键组合使用,默认情况下, 起始值为1,每次的增量为1,数值型,如果为浮点数,小数位数必须为0;

1、主键:PRIMARY KEY;

主键约束,每张数据表只能存在一个主键,主键保证记录的唯一性,主键自动为NOT NULL;主键的字段是可以被赋值的,但是不可以重复;

创建自动主键:id smallint unsigned auto_increment primary key;

        auto_increment,必须与primary key一起使用;

创建主键:id smallint unsigned primary key;

mysql> create table tb4(
    -> id smallint unsigned auto_increment primary key,
    -> username varchar(20) not null);
Query OK, 0 rows affected (0.09 sec)

mysql> describe tb4;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> insert tb4(username) values('tom');
Query OK, 1 row affected (0.10 sec)

mysql> insert tb4(username) values('t3m');
Query OK, 1 row affected (0.03 sec)

mysql> insert tb4(username) values('aam');
Query OK, 1 row affected (0.03 sec)

mysql> insert tb4(username) values('affm');
Query OK, 1 row affected (0.04 sec)

mysql> select * from tb4;
+----+----------+
| id | username |
+----+----------+
|  1 | tom      |
|  2 | t3m      |
|  3 | aam      |
|  4 | affm     |
+----+----------+
4 rows in set (0.00 sec)

mysql>

四、唯一约束:UNIQUE KEY;

唯一约束可以保证记录的唯一性,唯一约束的字段可以为null,每张数据表可以存在多个唯一约束;

mysql> create table tb5(
    -> id smallint auto_increment primary key,
    -> username varchar(20) unique key,
    -> age tinyint unsigned);
Query OK, 0 rows affected (0.09 sec)

mysql> show columns from tb5;
+----------+---------------------+------+-----+---------+----------------+
| Field    | Type                | Null | Key | Default | Extra          |
+----------+---------------------+------+-----+---------+----------------+
| id       | smallint(6)         | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)         | YES  | UNI | NULL    |                |
| age      | tinyint(3) unsigned | YES  |     | NULL    |                |
+----------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert tb5(username,age) values('tom2',23);
Query OK, 1 row affected (0.06 sec)

mysql> insert tb5(username,age) values('tom2',33);
ERROR 1062 (23000): Duplicate entry 'tom2' for key 'username'
mysql>

五、默认约束;默认值;default关键字;

mysql> create table tb6(
    -> id smallint unsigned auto_increment primary key,
    -> username varchar(20) not null unique key,
    -> sex ENUM('1','2','3') default '3');
Query OK, 0 rows affected (0.12 sec)

mysql> select * from tb6;
Empty set (0.00 sec)

mysql> show columns from tb6;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   | UNI | NULL    |                |
| sex      | enum('1','2','3')    | YES  |     | 3       |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert tb6(username) values('niha');
Query OK, 1 row affected (0.09 sec)

mysql> select * from tb6;
+----+----------+------+
| id | username | sex  |
+----+----------+------+
|  1 | niha     | 3    |
+----+----------+------+
1 row in set (0.00 sec)

六、约束:保证数据的完整性和一致性;如果约束只针对于一个字段来使用,称为列级约束,列级约束既可以在列定义时声明,也可以在列定义后声明;如果约束针对于两个以上的字段来使用,称为表级约束

约束类型:

NOT NULL(非空约束,不存在表级约束);PRIMARY(主键约束);UNIQUE(唯一约束);DEFAULT(默认约束,不存在表级约束);

FOREIGN KEY(外键约束):保持数据一致性,完整性,实现一对一或者一对多的关系,也是把数据较为关系型数据库的根本原因。

要求:

1、父表和子表必须使用InnoDB引擎,而且禁止使用临时表。需要在配置文件中配置默认引擎。default-storage-engine=INNODB

2、外键列和参照列必须具有相似的数据类型。其中数字的长度或是否有符号位必须相同;而字符的长度则可以不同;

3、外键列和参照列必须创建索引。如果外键列不存在索引的话,MySQL将自动创建索引。

创建外键约束:

mysql> create table provinces(
    -> id smallint unsigned primary key auto_increment,
    -> pname varchar(20) not null);
Query OK, 0 rows affected (0.12 sec)

mysql> create table users(
    -> id smallint unsigned primary key auto_increment,
    -> username varchar(20) not null,
    -> pid smallint unsigned,
    -> foreign key (pid) references provinces (id));

Query OK, 0 rows affected (0.07 sec)

查看表索引:

mysql> show indexes from provinces\G;

例:

mysql> create table provinces(
    -> id smallint unsigned primary key auto_increment,
    -> pname varchar(20) not null);
Query OK, 0 rows affected (0.12 sec)

mysql> create table users(
    -> id smallint unsigned primary key auto_increment,
    -> username varchar(20) not null,
    -> pid smallint unsigned,
    -> foreign key (pid) references provinces (id));
Query OK, 0 rows affected (0.07 sec)

mysql> show columns from users\g
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


mysql> show indexes from provinces\G;
*************************** 1. row ***************************
        Table: provinces
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
1 row in set (0.00 sec)

mysql> show indexes from users\G;
*************************** 1. row ***************************
        Table: users
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: users
   Non_unique: 1
     Key_name: pid
 Seq_in_index: 1
  Column_name: pid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
2 rows in set (0.10 sec)

ERROR:
No query specified

mysql>

外键约束的参照操作:实际操作中由于引擎的不同,一般不定义物理外键,而是逻辑外键,不是需要foriegn key这个关键词来定义。

用法: -> foreign key (pid) references provinces (id) on delete cascade

1、cascade:从父表删除或更新且自动删除或更新子表中匹配的行;

2、set null : 从父表删除或更新行,并设置子表中的外键列为NUll。如果使用该选项,必须保证子表列没有指定not null;

3、restrict:拒绝对父表的删除或更新操作。

4、no action:标准sql关键字,在MySQL中与restrict相同。

 

七、修改数据表

添加单列:alter table tb_name add [column] col_name column_definition [first | after col_name];

添加多列:alter table tb_name add [column] (col_name1 column_definition,.....); 不能指定列的位置,只能在表的下方;

mysql> show columns from users1;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type                 | Null | Key | Default | Extra          |
+-------+----------------------+------+-----+---------+----------------+
| id    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname | varchar(20)          | NO   |     | NULL    |                |
| pid   | smallint(5) unsigned | YES  | MUL | NULL    |                |
+-------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> alter table users1 add age tinyint not null default 10;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type                 | Null | Key | Default | Extra          |
+-------+----------------------+------+-----+---------+----------------+
| id    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname | varchar(20)          | NO   |     | NULL    |                |
| pid   | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age   | tinyint(4)           | NO   |     | 10      |                |
+-------+----------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table users1 add password varchar(20) not null after pname;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(4)           | NO   |     | 10      |                |
+----------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> alter table users1 add truename varchar(20) not null first;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| truename | varchar(20)          | NO   |     | NULL    |                |
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(4)           | NO   |     | 10      |                |
+----------+----------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> alter table users1 add (fex varchar(10) not null ,salary float(8,2) unsigned);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| truename | varchar(20)          | NO   |     | NULL    |                |
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(4)           | NO   |     | 10      |                |
| fex      | varchar(10)          | NO   |     | NULL    |                |
| salary   | float(8,2) unsigned  | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql>

删除单列:alter table tb_name drop [column] col_name;

删除多列:alter table tb_name drop col_name1, drop col_name2....;

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| truename | varchar(20)          | NO   |     | NULL    |                |
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(4)           | NO   |     | 10      |                |
| fex      | varchar(10)          | NO   |     | NULL    |                |
| salary   | float(8,2) unsigned  | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql> alter table users1 drop truename;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(4)           | NO   |     | 10      |                |
| fex      | varchar(10)          | NO   |     | NULL    |                |
| salary   | float(8,2) unsigned  | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> alter table users1 drop fex,drop age;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users1;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| pname    | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| salary   | float(8,2) unsigned  | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql>

添加主键约束:alter table tb_name add  primary key (id);

添加唯一约束:alter table tb_name add unique key (username);

添加外键约束:alter table tb_name add foreign key (pid) references provinces(id);

添加默认约束:alter table tb_name alter set col_name default col_value;

删除默认约束:alter table tb_name alter col_name drop default;

删除主键约束:alter table tb_name drop primary key;

删除唯一约束:alter table tb_name drop {index | key} index_name;

mysql> alter table users2 add id smallint unsigned;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   |     | NULL    |       |
| id       | smallint(5) unsigned | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table users2 add primary key (id);
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   |     | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table users2 add unique key(username);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table users2 add agg smallint unsigned;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| agg      | smallint(5) unsigned | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table users2 alter agg set default 20;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| agg      | smallint(5) unsigned | YES  |     | 20      |       |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table users2 alter agg drop default;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| agg      | smallint(5) unsigned | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>

修改数据表:

修改列定义:alter table tb_name modify [column] col_name column_definition [FIRST | AFTER col_name];

修改列名称: alter table tb_name change [column] old_col_name new_col_name column_definition [FIRST | AFTER col_name];

修改数据表的名字:

1、alter table tb_name rename [TO | AS] new_tb_name;

2、rename table tb_name to new_tb_name [ ,tb_name2 to new_tb_name2....];

mysql> alter table users2 modify id smallint unsigned not null first;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id       | smallint(5) unsigned | NO   |     | NULL    |       |
| username | varchar(20)          | NO   |     | NULL    |       |
| agg      | smallint(5) unsigned | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table users2 modify id tinyint unsigned not null;
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id       | tinyint(3) unsigned  | NO   |     | NULL    |       |
| username | varchar(20)          | NO   |     | NULL    |       |
| agg      | smallint(5) unsigned | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table users2 change agg age tinyint unsigned not null;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    15
Current database: t2

Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from users2;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| id       | tinyint(3) unsigned | NO   |     | NULL    |       |
| username | varchar(20)         | NO   |     | NULL    |       |
| age      | tinyint(3) unsigned | NO   |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> alter table users2 rename userName;
Query OK, 0 rows affected (0.09 sec)

mysql> show tables;
+--------------+
| Tables_in_t2 |
+--------------+
| provinces    |
| tb1          |
| tb2          |
| tb3          |
| tb4          |
| tb5          |
| tb6          |
| username     |
| users        |
| users1       |
+--------------+
10 rows in set (0.01 sec)

mysql> rename table username to users2;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+--------------+
| Tables_in_t2 |
+--------------+
| provinces    |
| tb1          |
| tb2          |
| tb3          |
| tb4          |
| tb5          |
| tb6          |
| users        |
| users1       |
| users2       |
+--------------+
10 rows in set (0.00 sec)

mysql>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值