码迷,mamicode.com
首页 > 其他好文 > 详细

对表的操作

时间:2018-05-12 23:55:24      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:one   col   默认   not   unique   mes   signed   表名   member   

-- 创建表

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


对表的操作

标签:one   col   默认   not   unique   mes   signed   表名   member   

原文地址:https://www.cnblogs.com/zhaojingyu/p/9030580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!