• MySQL——修改数据表


    1、添加单列:

    ALERT TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name

    其中tbl_name为表名,【】中的可不写,col_name为列名,column_definetion为列的属性约束,FIRST 表示添加的那一列位于所有列的前面,AFTER表示位于某一列的后面。

    例如:

      • ALERT TABLE user ADD age TINYINT NOT NULL DEFAULT 10;
      • ALERT TABLE user ADD password VARCHAR(32) NOT NULL AFTER username;

    2、添加多列:

    ALERT TABLE tbl_name ADD [COLUMN] (col_name column_definition,···)

    添加多列时无法指定位置关系。

    3、删除列:

    ALERT TABLE tbl_name DROP [COLUMN] col_name

    例如:

      • ALERT TABLE user DROP password;
      • ALERT TABLE user DROP password,DROP age;

    4、添加主键约束:

    ALERT TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,···)

    symbol为约束的名字。

    例如:

    ALERT TABLE user ADD CONSTRAINT PK_user_id PRIMARY KEY (id);

    5、添加唯一约束:

    ALERT TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,···)

    6、添加外键约束:

    ALERT TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,···) reference_definition

    例如:ALERT TABLE user1ADD FOREIGN KEY (pid) REFERENCE user (id);

    7、添加/删除默认约束:

    ALERT TABLE tbl_name ALERT [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}

    例如:ALERT TABLE user ALERT age SET DEFAULT 15;

    8、删除主键约束:

    ALERT TABLE tbl_name DROP PRIMARY KEY

    9、删除唯一约束:

    ALERT TABLE tbl_name DROP {INDEX|KEY} index_name

    10、删除外键约束:

    ALERT TABLE tbl_name DROP FOREIGN KEY fk_symbol;

    11、修改列定义:

      ALERT TABLE tbl_name MODIFY [COLUMN] col_name column_definition  [FIRST|AFTER col_name]

    例如:

    ALERT TABLE user MODIFY id SMALLINT UNSIGNED AUTO_INCREMENT FIRST;

    12、修改列名称:

    ALERT TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]

    13、修改数据表的名称:

    方法一:ALERT TABLE tbl_name RENAME [TO|AS] new_tbl_name

    方法二:RENAME TABLE tbl_name TO new_tbl_name [,tbl_name2 TO new_tbl_name2]···

  • 相关阅读:
    [leedcode 155] Min Stack
    [leedcode 154] Find Minimum in Rotated Sorted Array II
    [leedcode 153] Find Minimum in Rotated Sorted Array
    [leedcode 151] Reverse Words in a String
    [leedcode 150] Evaluate Reverse Polish Notation
    [leedcode 149] Max Points on a Line
    maven记录
    intelij idea常用功能介绍
    intelij idea设置和使用git
    intelij idea模板
  • 原文地址:https://www.cnblogs.com/liuyanping/p/4973030.html
Copyright © 2020-2023  润新知