一、普通CRUD
creat database 数据库名 [其他选项];
creat table 表名称(列声明);
增:Insert [into] 表名 [(列名1,列名2,列名3...)] value(值1,值2,值3,...);
删:delete from 表名称 where 删除条件;
改:update 表名称 set 列名称=新值 where 更新条件;
查:Select 列名 from 表名 [查询条件];
eg:
Insert into students value(null,”黄瑜宏”,”男”,20,”18126745520”);
insert into students (name, sex, age) values("孙丽华", "女", 21);
select * from students where sex="女";
where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询,
update students set tel=default where id=5;
delete from students where age<20;
二、创建表后的修改
添加列:
Alter table 表名 add 列名 列数据类型 [after 插入位置];
实例:
在表的最后追加列address: alter table students add address char(60);
在名为age的列后插入列birthday:alter table students add birthday date after age;
修改列:
Alter table 表名 change 列名称 列新名称 新数据类型
实例:
将students表中的tel列改名为telephone:alter table students change tel telephone cahr(13) default “-”;
将name列的数据类型改为char(16):alter table students change name name char(16) not null;
删除列:
Alter table 表名 drop 列名称
实例:
删除birthday列:alter table students drop birthday;
重命名表:
alter table 表名 rename 新表名;
示例:
重命名 students 表为 workmates: alter table students rename workmates;
删除整张表
drop table 表名;
示例: 删除 workmates 表: drop table workmates;
删除整个数据库
drop database 数据库名;
示例: 删除 samp_db 数据库: drop database samp_db;
Group by :分组,需要和聚合函数配合使用(如:max(), count(),avg()等),使用时至少一个分组标识字段
Order by: 排序,一般后面跟上某一个列名,然后查询到的会以该列名的大小顺序进行排名,默认的是ASC(升序),还有就是降序(desc)
Where:对查询到的内同进行筛选,where后面跟上限定条件,where使用在分组和排序的后面,但是执行顺序却在排序和分组之前。
Having:作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,
一般跟在group by 之后,执行记录组选择的一部分来工作。