mysql_数据表的修改操作

作者: supa同学 | 来源:发表于2018-02-24 17:59 被阅读2次
    • 删除列:ALTER TABLE tb1_name DROP[COLUMN] col_name;

    • 添加多列:ALTER TABLE tb1_name ADD[COLUMN] (col_name column_definition,...);

    • 添加单列:ALTER TABLE tb!_name ADD[COLUMN] col_name column_definition [FIRST|AFTER col_name];

    • 条件删除:DELETE FROM province WHERE id=3;

    • 添加主键约束 ALTER TABLE table_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)

    • 添加唯一约束 ALTER TABLE table_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] [index_type] (index_col_name, ...)

    • 添加外键约束 ALTER TABLE table_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES_definition;

    添加外键约束:ALTER TABLE users2 ADD FOREIGN KEY(pid) REFERENCES provinces (id);
    
    添加默认约束:ALTER TABLE users2 ALTER age SET DEFAULT 15;
    
    删除默认约束:ALTER TABLE users2 ALTER age DROP DEFAULT;
    

    修改定义列

    ALTER TABLE tb1_name MODIFY [COLUMN] col_name column_definition [FIRST|AFTER col_name]

    ALTER TABLE table_name id SAMLLINT UNSIGNED NOT NULL FIRST; // 把id放到第一列

    修改列名称

    ALTER TABLE table_name CHANGE [COLUMN] old_col_name new_col_name column_deinition [FIRST|AFTER col_name]

    ALTER TABLE users CHANGE pid p_id TINYINT UNSIGNED NOT NULL

    约束|修改表

    相关文章

      网友评论

        本文标题:mysql_数据表的修改操作

        本文链接:https://www.haomeiwen.com/subject/darqxftx.html