数据库:
数据库:存储数据的仓库
数据库管理系统:操作数据库的一个工具
关系型数据库的一些概念
数据库表:存储数据的一个单位
字段:字段是数据库表中存储一类数据的一个单位
元组:数据库中所有的数据
数据冗余:在数据库表中,多余的数据
失去数据完整性:如果某一列是唯一性数据,但是数据库中出现多个此数据
主键:具有唯一标注一条数据记录的列 ,此列没有任何业务意义,唯一的作用
就是能代表这条数据 ,主键绝对不能为空
数据库的结构化查询语言:
数据定义语言(DDL):
create(创建表、视图...)、alter(修改表、字段...)、drop(删除表、视图)
数据操作语言:
增加(insert)、删除(delete)、更新(update)、查询(select)
数据控制语言(DCL):
分配(grant)、回收(revoke)、拒绝(deny)
MySql操作:
mysql是使用数据库来操作表的,和sql servver类似,不同于oracle,oracle使用
用户进行管理表
在MySql里,如果改变数据库使用:
use 数据库名 ;
数据库的操作:
创建数据库:
create database dbname ;
删除数据库表:
drop database dbname ;
数据库表的管理:
MySql的数据类型:
主要包括以下五大类:
整数类型:BIT、BOOL、TINY INT、SMALL INT、MEDIUM INT、 INT、 BIG INT
浮点数类型:FLOAT、DOUBLE、DECIMAL
字符串类型:CHAR、VARCHAR、TINY TEXT、TEXT、MEDIUM TEXT、LONGTEXT、TINY BLOB BLOB、MEDIUM BLOB、LONG BLOB
日期类型:Date、DateTime、TimeStamp、Time、Year
其他数据类型:BINARY、VARBINARY、ENUM、SET、Geometry、PointMultiPoint、 LineString、MultiLineString、Polygon、GeometryCollection等
创建数据库表:
create table emp (
empid int primary key,
ename varchar(60),
sex char(4),
age int ,
salary double(8,2),
createdate date
)
alter table tableName add constraint T_PK primary key (id)
查看数据表:
DESC tablename;
查看表的详细结构:
show create table tableName \G "G 代表格式化"
插入数据:
insert into tableName(列1 ,列2...) values(v1,v2,......)
修改数据:
update tableName set { colName1=v1,colName2=v2,......}
删除数据:
delete from table where condition;
数据库查询:
查询表的全部行和列
select * from T_emp;
SELECT * FROM t_emp ;
SELECT ename,salary from t_emp;
SELECT ename as '姓名', salary as '薪资' from t_emp;
SELECT age from t_emp;
SELECT DISTINCT(age) from t_emp; // 显示年龄不重复的
SELECT * FROM t_emp WHERE empid < 3
SELECT * FROM t_emp LIMIT 4 ,2
SELECT ename ,salary ,salary * 2 FROM t_emp;
SELECT * from t_emp where ename = 'lucy' ;
SELECT * from t_emp where sex = '男';
SELECT * from t_emp where salary <> 3000
SELECT * from t_emp where sex ='男' and salary > 5000 and depart = '人事部';
SELECT * FROM t_emp where age BETWEEN 20 and 22 ;
SELECT * FROM t_emp where age >= 20 and age <= 22 ;
SELECT * from t_emp where age in (19,20,25,30)
SELECT * from t_emp where ename LIKE '%L%' ;
--查询姓张的
SELECT * from t_emp where ename like '张%'
SELECT * from t_emp where ename like '张_'
SELECT * from t_emp where ename like '李%'
SELECT * from t_emp where ename like 'ABO[1-9]'
SELECT * from t_emp where ename not like '李%'
SELECT * FROM t_emp where depart is null;
SELECT * FROM t_emp order by salary desc ,age asc
--查询数据表中共有多少条数据 、所有员工一年要发多少钱 ,每月员工平均薪资、最高工资、最低工资
SELECT
COUNT(empid) as '总数聚量' ,
sum(salary) * 12 as '总钱数',
avg(salary) as '平均薪资',
max(salary) as '最高',
min(salary) as '最低' ,
avg(age)
FROM
t_emp
where depart = '销售部'
--每个部门的平均工资
SELECT depart,avg(salary) as avgSalary from t_emp GROUP BY depart HAVING avgSalary > 5000
查询顺序:
From子句指定数据源
Where子句基于指定的条件进行筛选
Group by 子句将数据分为多个组 使用聚合函数进行计算
使用having子句再次进行筛选
使用order by 子句进行排序