对表的操作

-- 创建表

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

 
 

转载于:https://www.cnblogs.com/zhaojingyu/p/9030580.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值