Mysql DDL statement
- View database tables
show DATABASES;
- Enter the database
use test
--Build a table
CREATE TABLE stu(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT'student number',
sname VARCHAR(255) NOT NULL COMMENT'name',
age TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT'age',
gender ENUM('m','f ','n') not null DEFAULT'n
'
COMMENT'Gender ', intime DATETIME not null DEFAULT NOW() COMMENT'Enrollment time' )ENGINE INNODB CHARSET utf8mb4;
- View table
desc stu;
- Error when inserting, adjust sql_mode
SET global sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
- add column
ALTER TABLE stu ADD qq VARCHAR(20) NOT null DEFAULT '' COMMENT 'qq号';
- Add a column and specify the position of the column
ALTER TABLE stu add wechat VARCHAR(64) NOT NULL UNIQUE DEFAULT '' COMMENT '微信号' AFTER sname;
ALTER TABLE stu ADD num int not null UNIQUE DEFAULT 0 COMMENT'ID' FIRST;
- Delete column
ALTER TABLE stu DROP num;
- Modify column
ALTER TABLE stu MODIFY sname VARCHAR(64) NOT NULL COMMENT '姓名';
ALTER TABLE stu CHANGE gender sex CHAR(4) not null default '' comment '性别';
desc stu;
- Insert data
INSERT into stu VALUES(1,'zs','zs wechat', 18,'m',NOW(),'afdf');
INSERT INTO stu(sname,wechat,age,sex,qq) VALUES('ls','ls wechat',18,'m','ls qq');
-- Inquire
SELECT * from stu;