MySQL-表結構
阿新 • • 發佈:2019-01-09
- 建立新聞表
編號、標題、內容、釋出時間、點選量、是否置頂、分類、釋出人
CREATE TABLE IF NOT EXISTS cms_news(
id INT,
title VARCHAR(50),
content TEXT,
pubTime INT,
clickNum INT,
isTop TINYINT(1) COMMENT '0代表不置頂,1代表置頂'
);ENGINE=Innodb DEFAULT CHARSET=UTF-8;
- 列出所有表
SHOW TABLES;
- 查看錶結構
DESC cms_news; DESCRIBE cms_news; SHOW COLUMNS FROM cms_news;
- 向表中插入記錄 INSERT tbl_name VALUE|VALUES(值,·,·,)
INSERT cms_news VALUES(1,"This is Title","內容",'time'···);
- 主鍵
PRIMARY KEY
- 無符號的
UNSIGNED
- 自增長 AUTO_INCREMENT
CREATE TABLE IF NOT EXISTS user( id SMALLINT KEY AUTO_INCREMENT, username VARCHAR(20) )ENGINE=Innodb AUTO_INCREMENT=100 DEFAULT=utf-8; INSERT user VALUES(1,'king'); INSERT user(usename) VALUES('queen');
- 修改自增長的值
ALTER TABLE user AUTO_INCREMENT = 500;
- 非空 NOT NULL
CREATE TABLE IF NOT EXISTS user(
in INT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password CHAR(32) NOT NULL,
age TINYINT UNSIGNED
);
- 預設值 DEFAULT
CREATE TABLE IF NOT EXISTS user( in INT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL, password CHAR(32) NOT NULL, age TINYINT UNSIGNED DEFAULT 18, addr VARCHAR(50) NOT NULL DEFAULT '北京', sex ENUM('男','女','保密') );
- 唯一 UNIQUE KEY
CREATE TABLE IF NOT EXISTS user(
in INT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
password CHAR(32) NOT NULL,
card CHAR(18) UNIQUE
);
- 建立表
CREATE TABLE [IF NOT EXISTS] tal_name(
欄位名稱 欄位型別 [UNSIGNED|ZEROFILL] [NOT NULL] [DEFAULT 預設值] [[PRIMARY] KEY | UNIQUE [KEY]] [AUTO_INCREMENT]
);EGNINE=Innodb CHARSET=UTF-8 AUTO_INCREMENT=100;
- 建立使用者表user
CREATE TABLE IF NOT EXISTS user(
id SMALLINT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
password CHAR(32) NOT NULL DEFAULT '1Q2W3E4R',
email VARCHAR(50) NOT NULL,
age TINYINT UNSIGNED DEFAULT 18,
sex ENUM('男','女','保密') NOT NULL DEFAULT '保密',
addr VARCHAR(200) NOT NULL,
salary FLOAT(6,2),
regTime INT UNSIGNED,
face CHAR(100) NOT NULL DAFAULT 'default.jpg'
);
- 修改表名 user–>user11
ALTER TABLE user RENAME TO user11;
ALTER TABLE user RENAME AS user11;
ALTER TABLE user RENAME user11;
- 新增欄位 ADD
ALTER TABLE user ADD card VARCHAR(18);
ALTER TABLE user ADD test1 VARCHAR(100) NOT NULL UNIQUE;
- 指定欄位位置:
ALTER TABLE user ADD test2 VARCHAR(100) NOT NULL FIRST;
ALTER TABLE user ADD test2 VARCHAR(100) NOT NULL AFTER username;
- 刪除欄位
ALTER TABLE user DROP test1;
- 新增欄位同時刪除另一個欄位
ALTER TABLE user
ADD test1 VARCHAR(100) NOT NULL UNIQUE AFTER sex,
DROP addr;
- 修改欄位型別 MODIFY
ALTER TABLE user MODIFY email VARCHAR(200);
ALTER TABLE user MODIFY email VARCHAR(100) NOT NULL DEFAULT '[email protected]';
- 修改欄位位置 MODIFY
ALTER TABLE user MODIFY email VARCHAR(50) AFTER username;
- 修改欄位型別並且移動位置 MODIFY
ALTER TABLE user MODIFY addr VARCHAR(150) NOT NULL DEFAULT '北京' FIRST;
- 修改欄位名稱 CHANGE
ALTER TABLE user MODIFY addr address VARCHAR(150) NOT NULL DEFAULT '北京' FIRST;
- 給欄位新增預設值
ALTER TABLE user ALTER email SET DEFAULT '[email protected]';
- 給欄位刪除預設值
ALTER TABLE user ALTER age DROP DEFAULT;
- 新增主鍵
CREATE TABLE IF NOT EXISTS user1(
id INT;
card VARCHAR(18);
username VARCHAR(20) NOT NULL
);
ALTER TABLE user1 ADD PRIMARY KEY(id);
ALTER TABLE user1 ADD CONSTRAINT symbol PRIMARY KEY index_type(id);
- 複合主鍵
ALTER TABLE user1 ADD PRIMARY KEY(id,card);
- 刪除主鍵
ALTER TABLE user1 DROP PRIMARY KEY;
- 新增唯一索引
CREATE TABLE IF NOT EXISTS user2(
id TINYINT UNSIGNED KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
card CHAR(18) NOT NULL,
test VARCHAR(20) NOT NULL,
test1 CHAR(32) NOT NULL
);
ALTER TABLE user2 ADD NUIQUE(username);
ALTER TABLE user2 ADD CONSTRAINT symbol UNIQUE KEY uni_card(card);
- 給test和test1欄位新增聯合索引:
ALTER TABLE user2 ADD CONSTRAINT symbol UNIQUE INDEX muluni_test_test1(test,test1);
- 刪除唯一索引
ALTER TABLE user2 DROP INDEX username;
ALTER TABLE user2 DROP KEY uni_card;
ALTER TABLE user2 DROP KEY muluni_test_test1;
- 修改表的儲存引擎
ALTER TABLE user2 ENGINE=myisam;
- 刪除表
DROP TABLE user1;
DROP TABLE IF EXISTS user1;