对表的操作

Posted 随心朝阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对表的操作相关的知识,希望对你有一定的参考价值。

-- 创建表

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


以上是关于对表的操作的主要内容,如果未能解决你的问题,请参考以下文章

命令行,对表的操作

sql对表的操作

对表的操作

mysql对表的操作和数据库级别的外键

SQL - 对表的列和的操作

如何在 PL/SQL 中设置特定日期对表的操作执行?