标签:select def 修改 rds creat values 表格 结果 use
创建库确认修改后的表结构
mysql> desc t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| info | varchar(200) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改字段名
mysql> desc t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| info | varchar(200) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table t1 change info infofo char(20); 修改info列的名字及数据类型
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
验证修改后的结果
mysql> desc t1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| infofo | char(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
向表中插入一个新的字段
在最后一列插入新列
mysql> desc t3; 确认t3列当前字段
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(6) | NO | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table t3 add tel int(13); 插入一个tel列
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看插入后的列
mysql> desc t3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(6) | NO | | NULL | |
| name | varchar(10) | YES | | NULL | |
| tel | int(13) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
在表格开头插入新列
mysql> alter table t3 add sex char(1)first;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
在指定的列后面插入新列
mysql> alter table t3 add loc varchar(255) after name;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加字段时添加约束
mysql> alter table t3 add hobyy varchar(255) default ‘work‘;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加一个外键
T1表的结构
mysql> desc t1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| infofo | char(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
T3表的结构
mysql> desc t3;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| sex | char(1) | YES | | NULL | |
| id | int(6) | NO | | NULL | |
| name | varchar(10) | YES | | NULL | |
| loc | varchar(255) | YES | | NULL | |
| tel | int(13) | YES | | NULL | |
| hobyy | varchar(255) | YES | | work | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
将t3表的name列添加为t1表的name列的外键
mysql> alter table t3 add constraint t3_t1_name foreign key(name)references t1(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看t3表的变化
mysql> desc t3;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| sex | char(1) | YES | | NULL | |
| id | int(6) | NO | | NULL | |
| name | varchar(10) | YES | MUL | NULL | |
| loc | varchar(255) | YES | | NULL | |
| tel | int(13) | YES | | NULL | |
| hobyy | varchar(255) | YES | | work | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
删除外键
mysql> alter table t3 drop foreign key t3_t1_name;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table t3 drop key t3_t1_name;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除列
mysql> alter table t3 drop te1;
ERROR 1091 (42000): Can‘t DROP ‘te1‘; check that column/key exists
mysql> alter table t3 drop tel;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改列的顺序
mysql> alter table t3 modify name varchar(10) first;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除表
mysql> drop table t5; 直接删除
Query OK, 0 rows affected (0.00 sec)
mysql> drop table t5; 再次删除,由于已经删除了,所以表不存在会报错
ERROR 1051 (42S02): Unknown table ‘test01.t5‘
mysql> drop table if exists t5; 进行判断删除,if exists表示如果存在就删除
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show warnings; 记录的信息时不知道test01库中的t5表
+-------+------+---------------------------+
| Level | Code | Message |
+-------+------+---------------------------+
| Note | 1051 | Unknown table ‘test01.t5‘ |
+-------+------+---------------------------+
1 row in set (0.00 sec)
如果存在关联关系,需先删除关联关系,在删除表。
标签:select def 修改 rds creat values 表格 结果 use
原文地址:https://blog.51cto.com/14471717/2487271