CREATE TABLE TBName(name1 type1 NOT NULL PRIMARY KEY AUTO_INCREMENT,...);//创建表
USE TBName;//选择表
SHOW TABLES;//查看所有表
DROP TABLE TBName;//删除数据表
DELETE FROM TBName;//清空表中记录
DESC TBName; DESCRIBE TBName; SHOW COLUMNS FROM TBName;//查看User的表结构
ALTER TABLE pet ADD id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;//增加表的一列 ALTER TABLE pet DROP COLUMN des;//删除表的一列
ALTER TABLE TBName PRIMARY KEY(colName);//将表的一行设为主键(主键不可重复) ALTER TABLE TBName DROP PRIMARY KEY(colName);//将表的一行删除主键
RENAME TABLE pet TO animal;//变更表名
索引
1 2 3
CREATE INDEX idxName ON TBName(colName);//为一行设置索引(索引名字UNIQUE) DROP INDEX idxName ON TBName;//删除索引 注:索引是不可更改的,想更改必须删除重新建。
基本语法
1 2 3 4 5 6 7 8 9 10
查找:select * from table1 where field1 like ’%value1%’ //使用like来做相似查找 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 排序:select * from table1 order by field1,field2 [desc] 总数:select count(*) as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1[separator]