码迷,mamicode.com
首页 > 其他好文 > 详细

数据表的操作

时间:2020-04-15 09:26:08      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:select   def   修改   rds   creat   values   表格   结果   use   

创建库
mysql> create database test01;
Query OK, 1 row affected (0.00 sec)
切换至新库
mysql> use test01;
Database changed
创建一个带有主键约束的表
mysql> create table t1(
-> id int(10),
-> name varchar(10) primary key,
-> sex varchar(5),
-> info varchar(200)
-> );
Query OK, 0 rows affected (0.35 sec)
确定创建是否为主键
mysql> desc t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| info | varchar(200) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.48 sec)
使用主键约束
mysql> create table t2(
-> id int(10),
-> name varchar(10),
-> sex varchar(5),
-> primary key(id)
-> );
Query OK, 0 rows affected (0.00 sec)
确定是否有主键
mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
| sex | varchar(5) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
非空约束
mysql> create table t3(
-> id int(6) not null,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.01 sec)
查看表信息确认
mysql> desc 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> create table t7(
-> id int not null unique,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.00 sec)
查看其表结构
mysql> desc t7;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
设置列的默认值
mysql> create table t4(
-> id int(2) not null,
-> name varchar(20),
-> project varchar(20) default ‘mysql‘
-> );
Query OK, 0 rows affected (0.00 sec)
查看确认
mysql> desc t4;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(2) | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
| project | varchar(20) | YES | | mysql | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
设置自新增
mysql> create table t5(
-> id int not null primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.01 sec)
查看表结构进行确认
mysql> desc t5;
+-------+-------------+------+-----+---------+----------------+
| 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)
测试其自增效果
Mysql> insert into t5(name)values(‘zhangsan’),(‘lisi’);
mysql> select from t5;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
+----+----------+
2 rows in set (0.00 sec)
设置自增的起始值
mysql> create table t6(
-> id int primary key auto_increment,
-> name varchar(20)
-> ) auto_increment=5;
Query OK, 0 rows affected (0.01 sec)
插入数据进行测试
mysql> insert into t6(name) values(‘zhangsan‘),(‘lisi‘);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
验证其自增值:
mysql> select
from t6;
+----+----------+
| id | name |
+----+----------+
| 5 | zhangsan |
| 6 | lisi |
+----+----------+
2 rows in set (0.00 sec)
ALTER指令的使用
修改列值的数据长度
mysql> desc t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| sex | varchar(5) | YES | | NULL | |
| info | varchar(200) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table t1 modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 修改name字段的长度为20

确认修改后的表结构
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!