创建表
-- 创建表
create table 表名称1 (
字段1 字段类型 [default 值] [not null] auto_increment ,
字段2 字段类型 [default 值] [not null] ,
字段n 字段类型 [default 值] [not null],
primary KEY (字段1),
unique(字段2),
foreign key (字段n ) references 表名称2(表2的主键)
)auto_increment =1 --从1开始自增长
-- 主键:primary KEY
-- 唯一:unique
-- 非空:not NULL (只能写在列后面)
-- 检查:check(oracle专属)
-- 外键:foreign key 该表的外键必须是另外一张表的主键
复制表(备份)
复制表的结构和数据
create table 表名称 as select * from emp ;
只复制表的结构
create table 表名称 as select * from emp where 1=2;
注意:该种方法不能复制表的键
MySQL常用的数据类型:bit、int、bigint、decimal、varchar、date 、DATETIME、longtext、set、enum
数据库语言
DQL:数据库查询语言,select
DML:数据库操作语言,insert 、update、 delete
DDL:数据库定义语言,alter、 create 、drop等等
DQL
基本查询:
- select * from 表名称 where 条件 order by 字段;
- select 字段1,字段2,。。。字段n from 表名称;
限定查询:
- select 字段1,字段2,。。。字段n from 表名称 where 条件1 and 条件2 or 条件n;
分组查询: 常用的分组方法count 、avg 、sum 、min 、max
- select count(字段1),字段2,。。。字段n
from 表名称
where 条件1 and 条件2 or 条件n
group by 字段2,。。。字段n;
DML
增
插入所有数据
insert into 表名称 values(值1,值2.。。值n);
插入部分数据
INSERT into zpwd SELECT * from emp;
通过子查询插入多条数据(源表结构必须和目标表结构一致)
INSERT into zpwd (empno ,ename,deptno)SELECT empno ,ename,deptno from emp;
改
update 表名称 set 字段名称 = 值 [where 条件]
删
delete from 表名 [where ]
DDL
drop table 表名
create table text(
id int(5) auto_increment,
name varchar(50) default '张三',
primary key(id)
)
-- 修改表 alter table
-- ①修改列的长度
alter table text modify id int(10);
-- ②修改列的数据类型(前提:修改列必须全为空值和默认值)
-- (整型转字符串,可以直接转,没有前提)
alter table text modify id varchar(10);
-- ③添加删除约束
alter table text modify id varchar(10) not null;
alter table text add primary key(id);
-- ④重命名列和表
alter table text rename as text1
alter table text change id newid int(5);
-- ⑤添加或删除列
alter table text add age int(3) default 0 not null;
alter table text drop column age;
alter table text drop column age1,drop column age2;
面试题
drop、truncate 和 delete 表名 有什么区别
drop、truncate 属于ddl语言,后者属于dml语言,ddl语言属于隐式提交不能row back。drop是删除表,语句将表所占用的空间全释放掉;truncate截断表运行效率快,清除索引占用的表和数据,保留结构;delete可以删除表,但是保留该表的索引。
事务
概念:一组增删改。
关键字:commit、rowback
navicat默认是将所有增删改一次一提交。
set autocommit =false; 设置不自动提交
事务四大特性(ACID):
A(原子性):组成事务的增删改要么全成功要么全失败。
C(一致性):一旦事务结束,数据保持一致状态。
I(隔离性):事务之间互不影响。
D(永久性):事务一旦提交不能回滚。
注意点:
and 和 or 的优先级:and优先级高。
not in(无法使用null),in(无法使用null)
select 语句执行过程
-- 书写顺序:selelct from where group by having order by LIMIT
-- 执行顺序:from selelct where group by having order by LIMIT
-- ①where不能使用列别名
-- ②where不能使用分组函数
-- ③order by 可以使用列别名
-- ④不分组不允许单独使用having
索引
-- 优点:加快查询速度。
-- 缺点:降低增删改的数独。
-- 适用场景:在经常被作为查询条件的字段上建立索引。
视图
-- 视图是一个虚拟的表,里面没有任何数据,只是将常使用的查询存储起来,方便重复使用。
-- 视图也可以进行增删改,操作的是表!!!
-- 创建视图时加入with check option ,表示只有视图中查到的数据才可以增删改。
模糊查询会遇到的问题:
-- 通配符尽量不要用在date日期类型中,因为mysql中可以使用,但是移植到Oracle中就失效。
分页
select * from emp
-- where子句
-- order by
LIMIT 0,5 -- [0,5] 从索引位置开始,查询的个数。
创建序列
create table 表名称1 (
字段1 字段类型 [default 值] [not null] auto_increment,
字段2 字段类型 [default 值] [not null] ,
字段n 字段类型 [default 值] [not null],
primary KEY (字段1),
unique(字段2),
foreign key (字段n ) references 表名称2(表2的主键)
)auto_increment =1 --从1开始自增长
MySQL提供的函数
-- 函数
-- 虚表 dual表
-- abs() 绝对值
select ABS(-18) from dual;
-- ROUND(X,D) D是小数点后精确的位数(四舍五入)
select round(115),round(115,-1),ROUND(115.4455,3) from dual;
-- TRUNCATE`(X,D)截取,不四舍五入
select TRUNCATE(115,0),TRUNCATE(115,-2),TRUNCATE(115.4455,3) from dual;
-- SUBSTR() 截取字符串,索引值从1开始截取,[截取几个]
select SUBSTR('hello word',6),SUBSTR('hello word',6,3) from dual;
-- length(str) 返回字符串的长度
-- CONCAT(str1,str2,...) 字符串拼接,同oracle中的||
-- REPLACE(str,from_str,to_str) 替换将一个字符串中的某些字符用另外的字符替换
-- LPAD(str,len,padstr),RPAD(str,len,padstr) 左右填充
-- LTRIM(str),RTRIM(str) 左右去空格
-- 日期函数
mysql在5.6以后支持日期作为默认值。
CURDATE()
,CURRENT_DATE()
,CURRENT_TIME()
,NOW()
selectCURDATE(),CURRENT_DATE(),CURRENT_TIME(),NOW()
from dual;
--DATE_FORMAT(date,format)
格式化日期
-- %Y --年份
-- %m --月份
-- %M --月份(英文月份)
-- %d --日
-- %D --日(英文日期)
-- %h --时(12进制)
-- %H --时(24进制)
-- %i --分钟
-- %s --秒
select DATE_FORMAT(NOW(),'%M-%Y-%d ') from dual;
-- DATEDIFF(expr1,expr2) 返回两个日期相减相差多少天
select hiredate,DATEDIFF(NOW(),hiredate) from emp;
-- EXTRACT(unit FROM date) 提取时间
select hiredate,EXTRACT(year from hiredate) from emp;
-- TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) 按照year或month或day求差
-- 流程控制函数
-- case 。。。when。。。then
SELECT ename, sal, deptno, (
CASE deptno WHEN 10 THEN '开发部' WHEN 20 THEN '实施部'
WHEN 30 THEN '测试部' ELSE '小卖部' END ) dname
FROM emp;
--ifnull(相当于oracle中nvl)