zoukankan      html  css  js  c++  java
  • MySql表结构修改详解

    修改表的语法

    先创建两个表:

       表一: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 表名【查看表的创建代码】

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

    修改表添加外键约束

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

      alter table 表名 add constraint 约束名 foreign key (列名) references 关联表名(关联列名)

        eg:  alter table tbl_person add dept_id int(10) not null after p_id;(由于之前设计的表中没有该字段,所以先添加,若有的话就忽略该步)

               alter table tbl_person add constraints fk_1 foregin key(dept_id) references tbl_department(dept_id);

  • 相关阅读:
    关于类的继承与初始化顺序
    大数据协作框架
    关于委托和线程
    Hive高级
    聚集索引和非聚集索引
    Hadoop生态优秀文章集锦
    hive深入使用
    hive常规配置及常用命令使用
    HBase核心技术点
    zipkin环境搭建
  • 原文地址:https://www.cnblogs.com/sirab415/p/7163655.html
Copyright © 2011-2022 走看看