码迷,mamicode.com
首页 > 数据库 > 详细

MySql表结构修改详解

时间:2017-07-13 23:42:09      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:word   des   查看   primary   alt   prim   bsp   extra   field   

修改表的语法

先创建两个表:

   表一:tbl_department 部门表

         create table tbl_department (

                        dept_id int(10) not null unsigned auto_increment,

                        dept_name varchar(20) not null,

                        dept_describ varchar(100) ,

                        primary key(dept_id)

          )

   表二:tbl_person 人员信息表

       create table tbl_person(

                       p_id int(10) not null unsigned auto_increment,

                       p_username varchar(20) not null,

                       p_password varchar(20) not null,

                       p_email varchar(50) not null,

                       create_date datetime not null,

                       primary key(p_id)

          )

=============================================

增加列【add 列名】

=============================================

1.   alter table 表名 add 列名 列类型 列参数【加的列在表的最后面】

       eg: alter table tbl_person add dept_id int(10) not null;

2.   alter table  表名 add 列名 列类型 列参数 after 某列【把新列加在某列后面】

       eg:alter table tbl_person add dept_id int(10) not null after p_email;

3.   alter table  表名 add  列名 列类型 列参数 first【把新列加在最前面】

       eg: alter table tbl_person add dept_id int(10) not null first;

=============================================

删除列【drop 列名】

=============================================

1. alter table 表名 drop 列名;

    eg: alter table tbl_person drop address;

=============================================

修改列【modify 列名/change 旧列名 新列名】

=============================================

1. alter table 表名 modify 列名 新类型 新参数【修改列类型】

  eg: alter table tbl_person modify create_date varchar(50);

2. alter table 表名 change 旧列名 新列名 新类型 新参数【修改列类型和列参数】

  eg: alter table tbl_person change p_username username varchar(20);

=============================================

查询列【desc 表名】

=============================================

1.desc tbl_department;

+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| p_id | int(4) | NO | PRI | NULL | auto_increment |
| p_uname | varchar(20) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
| dep_id | int(4) | NO | | NULL | |
| create_date | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+

2.show columns from tbl_department;

+-------------+-------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| p_id | int(4) | NO | PRI | NULL | auto_increment |
| p_uname | varchar(20) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
| dep_id | int(4) | NO | | NULL | |
| create_date | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+

3.show create table 表名【查看表的创建代码】

 

 

MySql表结构修改详解

标签:word   des   查看   primary   alt   prim   bsp   extra   field   

原文地址:http://www.cnblogs.com/sirab415/p/7163655.html

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