对表的操作

-- 创建表

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


posted @ 2018-05-12 23:27  随心朝阳  阅读(312)  评论(0编辑  收藏  举报