mysql创建三个表_mysql三:表操作
示例:
mysql> usedb2
Readingtable information for completion of table and columnnames
You can turnoff this feature to get a quicker startup with -ADatabasechanged
mysql> create tableservice;
ERROR1113 (42000): A table must have at least 1 columnmysql> create table service(id int);
Query OK,0 rows affected (0.02sec)
#1、修改存储引擎
mysql> alter tableservice-> engine=innodb;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> create table student10(id int);
Query OK,0 rows affected (0.01sec)
#2、添加字段
mysql> alter tablestudent10-> add name varchar(20) not null,-> add age int(3) not null default 22;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter tablestudent10-> add stu_num varchar(10) not nullafter name; # after关键词,添加到name字段后
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter tablestudent10-> add sex enum('male', 'female') default 'male'first; # first关键词,添加到最前面
Query OK,0 rows affected (0.01sec)
Records:0 Duplicates: 0 Warnings: 0mysql> select * fromstudent10;
Emptyset (0.00sec)
mysql>describe student10; # 查看表结构+---------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+-------+
| sex | enum('male','female') | YES | | male | |
| id | int(11) | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | NO | | 22 | |
+---------+-----------------------+------+-----+---------+-------+
5 rows in set (0.00sec)
#3、删除字段
mysql> alter tablestudent10-> dropsex;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter table service add mac varchar(20);
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter table service dropmac;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe student10;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | NO | | 22 | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00sec)
#4、修改字段类型modify
mysql> alter tablestudent10-> modify age int(3);
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter tablestudent10-> modify id int(11) not null primary keyauto_increment; # 修改为主键
Query OK,0 rows affected (0.04sec)
Records:0 Duplicates: 0 Warnings: 0mysql> select * fromstudent10;
Emptyset (0.01sec)
mysql>describe student10;+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00sec)
#5、增加约束(针对已有的主键添加auto_increment)
mysql> alter table student10 modify id int(11) not null primary keyauto_increment;
ERROR1068 (42000): Multiple primary keydefined
mysql> alter table student10 modify id int(11) not nullauto_increment;
Query OK,0 rows affected (0.01sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe student10;+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00sec)
#6、对已经存在的表添加复合主键
mysql> alter tableservice-> add host_ip varchar(24),-> add port int(4);
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe service;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| host_ip | varchar(24) | YES | | NULL | |
| port | int(4) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00sec)
mysql> alter tableservice-> add primary key(host_ip, port); # 增加复合主键,经测试必须是这个字段存在
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe service;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| host_ip | varchar(24) | NO | PRI | NULL | |
| port | int(4) | NO | PRI | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00sec)
#7、删除主键
# a.删除自增约束
mysql>describe student10;+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00sec)
mysql> alter tablestudent10-> modify name varchar(10) not null primary key;
ERROR1068 (42000): Multiple primary keydefined
mysql> alter tablestudent10-> drop primary key;
ERROR1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a keymysql> alter tablestudent10-> modify id int(11) not null;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe student10;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00sec)
# b.删除主键
mysql> alter tablestudent10-> drop primary key;
Query OK,0 rows affected (0.03sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe student10;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01sec)
#8、添加主键(和删除主键挪移了位置,因为不想新增一个表)
mysql> alter tablestudent10-> modify name varchar(10) not null primary key;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql>describe student10;+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01sec)
#9、添加主键和自动增长(多主键是不允许的,主键是唯一,非空的但是可以多个字段联合成一个主键)
mysql> alter tablestudent10-> modify id int not null primary keyauto_increment;
ERROR1068 (42000): Multiple primary keydefined
mysql> alter tablestudent10-> drop primary key;
Query OK,0 rows affected (0.02sec)
Records:0 Duplicates: 0 Warnings: 0mysql> alter tablestudent1-> modify id int not null primary keyauto_increment;
ERROR1146 (42S02): Table 'db2.student1' doesn't exist
mysql> alter table student10
-> modify id int not null primary key auto_increment; # 添加主键和自动增长成功
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student10;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| stu_num | varchar(10) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql创建三个表_mysql三:表操作相关推荐
- mysql 创建表字段长度范围_Mysql的建表规范与注意事项
一. 表设计规范 库名.表名.字段名必须使用小写字母,"_"分割. 库名.表名.字段名必须不超过12个字符. 库名.表名.字段名见名知意,建议使用名词而不是动词. 建议使用Inno ...
- mysql怎么删除表中字段的数据库表_Mysql 数据库 表 字段的创建 修改 删除
MYSQL基础上机练习题(一) 数据库.表.字段的创建.修改.删除 一.实验目的: 创建.修改.删除数据库 创建.修改.删除表 创建.修改.删除字段 二.内容: 创建一个用于企业管理的员工管理数据库, ...
- mysql连接查询作业答案_MySQL连表查询练习题
1.建库 库名:linux50 字符集:utf8 校验规则:utf8_general_ci  create database linux4 charset utf8 default collate ...
- mysql 用户管理表_Mysql—用户表详解(mysql.user)
MySQL 数据库 Mysql-用户表详解(mysql.user) MySQL是一个多用户管理的数据库,可以为不同用户分配不同的权限,分为root用户和普通用户,root用户为超级管理员,拥有所有权限 ...
- mysql如何分表_MySQL分表和分区的具体实现方法
垂直分表 垂直分表就是一个包含有很多列的表拆分成多个表,比如表A包含20个字段,现在拆分成表A1和A2,两个表各十个字段(具体如何拆根据业务来选择). 优势:在高并发的情境下,可以减少表锁和行锁的次数 ...
- mysql中调用多个表_MySQL 多表查询
文章转载的:http://www.cnblogs.com/BeginMan/p/3754322.html 一.多表查询方法分类 1.交叉连接查询(得到的是两个表的乘积,类似于矩阵乘积) select ...
- mysql++快速复制大表_MySql数据库表快速复制
表索引操作 这里之所以写索引的操作主要原因在于:在导入数据前删除索引能大大提升导入速度. DDL语句获取现有索引1show create table t_question_bak; 获取所有索引 获取 ...
- mysql course表_MySQL多表查询初探
今天是我时隔五年再次动起键盘写文章,距离我上一次写文章,初中作文......所以我的文章并没有雕章琢句,更不会惊天地泣鬼神,只要大家能学到知识,我就很开心了.呀丫丫......又开始罗嗦一大堆没用的了 ...
- mysql左驱动表_MySQL多表联表查询驱动表选择
联表查询做开发的小伙伴会经常使用,但是可以大家都比较少去深入了解MySQL是怎么执行多表联表查询的,比如怎么选择驱动表(第一个被处理的表),是先联表还是说先根据where条件(前提是有where条件) ...
- mysql建立班级表_mysql数据表设计-班级表 学生表 老师表 课程表 成绩表
mysql数据表设计- 班级表 学生表 老师表 课程表 成绩表 image.png /* Navicat Premium Data Transfer Source Server : localhost ...
最新文章
- 两个月入门深度学习,全靠动手实践!一位前端小哥的经验分享
- 从这个11.11开始,终结数据结构与算法的噩梦
- 在二分类问题中,准确率一直处于50%上下的解决方法
- Python列表操作:统计列表中的重复项出现的次数的方法,不懂也要知道的python知识
- python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)
- 泛型方法的定义和使用_泛型( Generic )
- 软件:常用 Linux 软件汇总,值得收藏!
- debian 升级linux内核,Debian8升级内核到4.5
- 用来做 favicon 的站点
- cc2530定时器和捕获比较_ALIENTEK 阿波罗 STM32F767 开发板资料连载十四章 输入捕获实验...
- android app反解工具,安卓反编译逆向工具:Apktool + dex2jar 教程
- DB9公头母头接口定义及连接
- 百度问答怎么引流,百度知道引流技巧
- 东莞软件开发公司有哪些【怎么样】
- 百度音乐助手 下载高品质音乐
- 【查看Python导入包的地址】
- 3、RDA8910(4GCAT1)CSDK二次开发:GPIO输入详解
- 积分使用及有效期管理
- 此nvidia驱动程序与此windows版本不兼容
- py.test --pep8 vsearch.py报错解决办法