字符串型
CHAR,VARCHAR,BINARY,VBINARY,TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOG,TINYTEXT,TEXT,MEDIUMTEXT,LONGTEXT,EMUM,SET
日期时间型
date,time,datetime,timestamp
数据限定修饰:
NOT NULL,NULL,DEFAULT,AUTO_INCREMENT,UNSIGNED,PRIMARY KEY,UNIQUE KEY,FOREIGN KEY
CHARACTER SET #ps:SHOW CHARACTER SET 显示当前数据库所支持的所有字符集
COLLATION #ps:SHOW COLLATION 显示所支持的所有排序规则
以下是SQL的每种DDL,DCL,DML语言
DDL
----Data Definition Language 数据库定义语言 如 create procedure之类
创建数据库
CREATE DATABASE [IF NOT EXISTS] DBNAME [CHARACTER SET 'CHAR_NAME'] [COLLATE 'COLL_NAME']
修改:ALTER 删除:DROP
创建一张新表
CRTATE TABLE [IF NOT EXISTS] TBNAME(col_name col_definition,...)
EXAMPLE:
mysql>CREATE TABLE students(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) UNIQUE KEY NOT NULL,Age TINYINT UNSIGNED INDEX,Gender CHAR(1) NOT NULL) [ENGINE={MyISAM | InnoDB }];
也可以这样写(区别在于单独定义主键,唯一键和索引):
mysql>CREATE TABLE students(Id INT NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) NOT NULL,Age TINYINT UNSIGNED,Gender CHAR(1) NOT NULL,PRIMARY KEY(id),UNIQUE KEY(name),INDEX(age))
查询出一张表的数据后创建新表(字段定义会丢失,数据会保留)
CREATE TABLE TBNAME SELECT...
EXAMPLE:
mysql>CREATE TABLE test SELECT * FROM students WHERE Id>5;
以一张表的格式定义,创建一张新的空表
CREATE TABLE TBNAME1 LIKE TBNAME2
修改表:
ALTER TABLE tb_name
MODIFY #修改字段定义
CHANGE #可以修改字段名和字段定义
ADD
DROP
EXAMPLE:
给表添加字段
mysql>ALTER TABLE students ADD (course VARCHAR(100),teacher CHAR(20));
mysql>ALTER TABLE students CHANGE course Course VARCHAR(100) [AFTER Name];
重命名表名
mysql>ALTER TABLE students RENAME TO stu;
mysql>RENAME TABLE stu TO students;
添加一个外键约束
ALTER TABLE students ADD FOREIGN KEY foreign _cid (CID) REFERENCES course (CID);
创建索引
CREATE INDEX index_name ON TABLE (col_name[(length)] [ASC|DESC]) [USING {BTREE|HASH}];
删除索引
DROP INDEX index_name ON TBNAME;
查看表状态:SHOW STATUS LIKE 'TBNAME';
查看表的索引:SHOW INDEXES FROM TBNAME;
DML
----Data Manipulation Language 数据操纵语言如insert,delete,update,select(插入、删除、修改、检索)插入修改数据
#如果每个字段都有值,不需要写字段名称,每组值用,隔开
mysql>INSERT INTO tb_name (col1,col2) VALUES ('STRING',NUM),('STRING',NUM);
mysql>INSERT INTO tb_name SET col1='string',col2='string';
mysql>INSERT INTO tb_name (col1,col2,col3) SELECT...;
EXAMPLE:
mysql>INSERT INTO students (Name,Gender,teacher) VALUE ('lujunyi','M','mage'),('wusong','M','zhuima');
mysql>INSERT INTO students SET Name='lujunyi',Gender='M',tearcher='zhuima';
更新数据
mysql>UPDATE tb_name SET column=value WHERE column=value;
mysql>UPDATE students SET Course='mysql' WHERE Name='lujunyi';
替换数据:
和UPDATE使用方式一样,只要将UPDATE换成REPLACE即可
删除数据
mysql>DELETE FROM tb_name WHERE conditions;
mysql>DELETE FROM students WHERE Course='mysql';
清空表:将会重置计数器
mysql>TRUNCATE tb_name
查询数据
单表查询:
mysql>SELECT [DISTINCT] column FROM tb_name WHERE CONDITION;
EXAMPLE:
#基本投影查询
mysql>SELECT Name,teacher FROM students WHERE Name='wusong';
#重复的结果只显示一次
mysql>SELECT DISTINCT Gender FROM students;
#组合条件,可以使用AND,OR,NOT,XOR组合多个条件
mysql>SELECT * FROM students WHERE Age>20 AND Gender='M';
#使用BETWEEN...AND...筛选出年龄介于20-25之间的数据
mysql>SELECT * FROM students WHERE Age BETWEEN 20 AND 25;
#查询Name以Y开头的的数据,%表示任意长度的任意字符,_表示任意单个字符
mysql>SELECT * FROM student WHERE Name LIKE 'Y%';
#使用正则表达式匹配查询,关键词为RLINK或者REGEXP
mysql> SELECT * FROM students WHERE Name RLINK '^[MNY].*$';
#使用IN关键词,将条件限定在一个列表中。用IS关键词,表示条件是否为空(IS NULL 或者 IS NOT NULL)
mysql>SELECT * FROM students WHERE Age IN (20,22,24);
#将查询的结果进行排序
mysql>SELECT * FROM students ORDER BY Name {ASC|DESC};