【资料图】
——后续的内容将会记录作者在计科学习内容
RDBMS(关系型数据库):建立在关系模型基础上,有多张相互连接的二维表组成的数据库
特点:
SQL的通用语法:
Show databases;
Select database ();
Creat database [if not exists]数据库名 [default charset 字符集] [collate排序规则];
drop database[if exists]数据库名;
use 数据名;
Show tables;
desc 表名;
Show create table 表名;
DDL——表操作——创建:
Create table 表名( 字段1 字段1类型[comment 字段1注释], 字段2 字段2类型[comment 字段2注释])[comment 字段注释];
字符串类型:
Alter table 表名 add 字段名 类型(长度)[comment注释][约束]
Alter table 表名 modify 字段名 新数据类型(类型)
Alter table 表名 change 旧字段名 新字段名 类型(长度)[comment注释][约束]
Alter table 表名 drop 字段名;
Alter table 表名 rename to 新表名;
drop table [if exsits]表名;
truncate table 表名;
Insert into 表名 (字段名1,字段名2...)Values(值1,值2...);
Insert into 表名 values(值1,值2...);
Insert into 表名(字段名1,字段名2)values(值1,值2...),(值1,值2...);Insert into 表名 values(值1,值2...),(值1,值2...);
update 表名 set 字段名1=值1,字段名2=值2,...[where条件];
delete from 表名[where 条件];
select 字段1,字段2...from 表名;select *from 表名;
select 字段1 [As 别名1],字段1 [As 别名1]...from 表名;
select distinct 字段列表 from 表名;
> >= < <==<>或!=between...and在某个范围之内(含最小值,最大值)in(...)在in之后的列表中的值,多选一Like 占位符,模糊匹配(匹配单个字符,%匹配任意字符): select * from 表名where name Like "%666"is Null是Nulland或&&OR或||NOT或!
select 聚合函数(字段列表) from 表名;select count(*) from employee;
select 字段列表 from 表名 [where条件] group by 分组字段名[having 分组后过滤条件];
where与having区别
1例如,根据性别分组,统计男性员工和女性员工的数量select gender count(*) from employment group by gender;
2例如,根据性别分组,统计男性员工和女性员工的平均年龄select gender avg(age) from employee group by gender
3例如,年龄小于45的员工,并根据工作地址分组,获取员工数大于等于3的工作地址select count(*) from employee where age<45 group by workdress having count(*)>=3
select 字段列表 from 表名 order by 字段1 排序方式1,字段2,排序方式2;
select 字段列表 from 表名 limit 启始索引,查询记录数;
关键词: