zoukankan      html  css  js  c++  java
  • 对表的操作

    -- 创建表

    DROP TABLE  IF EXISTS member;
    CREATE TABLE member(
        id int(11) PRIMARY KEY AUTO_INCREMENT,
        regName VARCHAR(10) DEFAULT NULL UNIQUE KEY,
        pwd VARCHAR(16) NOT NULL,
        mobilePhone CHAR(11),
        type TINYINT(1) DEFAULT 1,
        leaveAmount DOUBLE(18,2) DEFAULT 0.00,
        regTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
    );

    -- 1:新增表字段

    ALTER TABLE member ADD regTime  TIMESTAMP DEFAULT CURRENT_TIMESTAMP();
    ALTER TABLE member ADD type TINYINT DEFAULT 1 AFTER mobilePhone;
    ALTER TABLE member ADD regTime  TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),ADD type TINYINT DEFAULT 1 AFTER mobilePhone;

    -- 2:删除表字段

    ALTER TABLE member DROP type;
    ALTER TABLE member DROP regTime,ADD type TINYINT DEFAULT 1 AFTER mobilePhone;
    ALTER TABLE member DROP type,DROP leaveAmount;

    -- 3:修改表字段
    -- type tinyint 默认是1
    -- 将type int ,默认值为2

    ALTER TABLE member MODIFY type INT UNSIGNED DEFAULT 2;
    ALTER TABLE member MODIFY type TINYINT UNSIGNED DEFAULT 1 FIRST;

    -- 修改默认值

    ALTER TABLE member MODIFY type TINYINT UNSIGNED DEFAULT 2;
    ALTER TABLE member ALTER type SET DEFAULT 3;
    ALTER TABLE member ALTER type DROP DEFAULT;

    -- 4:修改列名,字段名

    ALTER TABLE member CHANGE COLUMN regTime createTime TIMESTAMP;

    -- 5:修改表名

    ALTER TABLE member RENAME member2;
    RENAME TABLE member2 TO member;

    -- 6:增加约束

    ALTER TABLE member ADD PRIMARY KEY (id);
    ALTER TABLE member ADD UNIQUE KEY (regName);
    ALTER TABLE member ADD UNIQUE (regName);
    ALTER TABLE loan ADD CONSTRAINT fk_id FOREIGN KEY(memberID) REFERENCES member(id);
    ALTER TABLE member ALTER type SET DEFAULT 3;

    -- 7:删除约束

    ALTER TABLE member DROP PRIMARY KEY;
    SHOW INDEXES FROM member;
    ALTER TABLE member DROP INDEX regName;

    SHOW CREATE TABLE loan;
    -- loan_ibfk_1
    ALTER TABLE loan DROP FOREIGN KEY loan_ibfk_1;

    
    

    ALTER TABLE member ALTER type DROP DEFAULT

    
    
  • 相关阅读:
    Hive学习之路 (十二)Hive SQL练习之影评案例
    Hive学习之路 (十一)Hive的5个面试题
    Guava Files 源码分析(二)
    Guava Files 源码分析(一)
    观察者模式与Guava EventBus
    Guava 源码分析之 Beta, GwtCompatible, GwtIncompatible, Charset, HashCode
    Java IO的简单示例
    从volatile说到i++的线程安全问题
    乐观锁与悲观锁
    SimpleDateFormat的线程安全问题与解决方案
  • 原文地址:https://www.cnblogs.com/zhaojingyu/p/9030580.html
Copyright © 2011-2022 走看看