一、数据库,数据库管理系统概述
二、mysal安装指令
mysal安装:mysqld --initialize --console
mysal服务安装:mysqld --install mysql
三、SQL语言
1.DLL操作数据库、表
创建数据库:create database 数据库名 character set 编码方式 collate 排序规则;
查看数据库:show databases;(当前数据库服务器中所有数据库)
查看数据库创建信息:show create database 数据库名;
修改数据库: alter database 数据库名 character set 编码方式;
删除数据库: drop database 数据库名;
切换数据库:use 数据库名;
查看当前数据库:select database();
创建表:create table 表名( 列名1 数据类型 (约束),列名2 数据类型 (约束),列名3 数据类型 (约束));
查看表:show tables;(当前库中所有表)
查看表创建细节:show create table 表名;
修改表名:alter table 旧表名 rename 新表名;
删除表: drop table 表名;
查看表内容:desc 表名;
增加表内容:alter table 表名 add 列名 数据类型;
修改表内容:alter table 表名 change 旧列名 新列名 新数据类型;
删除表内容:alter table 表名 drop 列名;
修改表字符集:alter table 表名 character set 编码方式;
2.DML对表中数据操作
3.DQL数据查询
简单查询:select * from 表名;( * 代表全部,可查询指定列将*换成想查询列名,多个逗号隔开)
条件查询:where后条件可有 =,!=,<>,<,<=,>,>=,between...and...,in(set),is null,and,or,not
如:select * from 表名 where 列名=值 ;
其中in()用于查询单列多值:select * from 表名 where 列名 in('值1','值2','值3');
模糊查询:like' 表达式 ' ,_任意一个字符 ,%任意0~n个字符
查询第二个字母为i的数据:select * from 表名 where 列名 like'_i%';
字段控制查询:DISTINCT去除重复记录,做加运算时二者中有null空值会出错可用ifnull(列名,0)将null转换为数值0
DISTINCT:select DISTINCT sal from emp;
ifnull(列名,0):select*,列名1+ifnull(列名2,0) from 表名;
排序:select * from 表名 order by 列名 desc; (desc降序,asc升序(默认))
当列名1相同时按列名2排序: select * from 表名 order by 列名1 desc,列名2 asc;
聚合函数:count(*)纵向统计不为null个数,sum()求和,avg()均值,最大max(),最小min()
count(*):select count(列名1),count(列名2) from 表名;
sum(): select sum(列名) from 表名;
avg():select avg(列名) from 表名;
max()和min():select MAX(列名),MIN(列名) from 表名;
分组查询:group by 如果查询语句中有分组操作,则select后面能添加的只能是聚合函数和被分组的列名
如:select 分组列名,sum(列名) from 表名 group by 分组列名;
HAVING子句:用于在执行聚合查询后筛选结果集,仅能在包含聚合函数的 select 语句中使用 ,它在由 group by 子句创建的分组上设置条件,作用等同于where
如:select 分组列名, sum(列名1) from 表名 group by 分组列名 HAVING sum(列名1) > 100;
LINIT:限定查询结果的起始行以及总行数
如从第三行开始查询五行:select* from emp LIMIT 3, 5;