2018-06-25 数据库
mysql
显示当前所有的数据库
show databases
支持模糊查询、_表示占位符,加反斜杠转义
show databases like "db%";
show databases like "db/_%";
显示当前所有的数据库
use dbName;
显示当前数据库所有表、支持模糊查询
show tables like "t%"
描述表的结构
desc\describe tableName;
描述表的结构
show columns from t_user;
显示当前所在的数据库
select database();
返回当前时间
select now();
返回当前数据库的版本信息
select version();
创建数据库:
创建数据库:
create database dbName [[default] charset 'utf8']
删除数据库:
drop database dbName;
修改数据库:
alter database dbName default charset gbk;
查看数据库定义:
show create database dbName;
表的常见操作
表的创建:
create table tName(
field1 int primary key auto_increment,
gender varchar(20) not null
) default charset utf8 engine=innodb/mysiam
查询表创建的信息:
show create table tname[\G];
对表结构的修改:
修改表名:
rename table oldTableName to newTableName;
alter table tableName rename to new TableName;
修改表的属性(字符集|引擎……):
alter table tableName charset=gbk
alert table t_test charset='gbk';
修改表的字段:
alter table tName change/modify/drop/add field
增加一个新的字段:
alter table tName add newColumn condition 位置
alter table t_test add nickname varchar(50) not null unique
修改一个字段的名称
alter table tName change nickname test varchar(30) default 'liushuaige' first;
修改一个字段的类型
alter table tName modify test varchar(80) default 'hehe' after age;
删除一个字段
alter table t_test drop test;
CRUD(C:create 增加 R:retrieve 查询 U:update 修改 D:delete 删除):
insert into
tableName(field1,field2,……,fieldn)
values(val1,val2,……,valn);
update tableName set field1=newV[,……,fieldn=newV]
where condition;
delete from tableName where condition;
truncate [table] tableName; #慎用
select *[field1,field2,……,fieldn] from table [where condition]
10、单表的条件查询
关系运算符: > < >= <= = != <>
逻辑运算符:and(并且) or(或者) not
空和非空的判断: is null is not null
between and in not in
排序:
order by field1 [asc|desc] [,field2 [asc|desc]]
分组:
group by field [having condition]
分页:
limit offset, pageSize;
11、编码问题
utf8
gbk
latin1
……
校对集:
_bin: binary 区分大小写
_cs: 区分大小写
_ci: 忽略大小写
=============================================================
今天目标:
分表:
多表关联查询:
外键:
视图:
索引:
面试题:
1、说说你所知道的mysql的数据库引擎有哪些?
2、innodb、myisam 两个引擎的区别?
=============================================================
在设计数据库时,一般而言要求遵循数据库三范式(3NF)
1、设计的字段具有原子性(字段不可分割)
2、每天记录具有唯一性 主键
3、外键
大量数据冗余
create TABLE emp (
id int PRIMARY KEY auto_increment,
name varchar(50) not null,
gender char(10) DEFAULT '男',
tel varchar(20) DEFAULT '110',
address varchar(255),
qq varchar(30),
age int DEFAULT 18,
deptId int,
# 添加外键约束
constraint foreign key(deptid) references dept(id) on delete set NULL on update
)
create table dept(
id int PRIMARY KEY auto_increment,
name varchar(50) not null UNIQUE,
describle text
)
# 多表管理查询
# 多表管理查询有五种连接方式
# 1、交叉查询
select *
from dept,emp
SELECT * from emp cross join dept;
# 笛卡尔积现象
# 内连接
SELECT *
FROM emp,dept
WHERE emp.deptid = dept.id
SELECT e.*,t.name as deptname, t.describle deptdesc from emp as e inner join dept t on (e.deptid = t.id)
# 外连接
# 外连接分为左外连接和右外连接
SELECT e.*,d.name deptname, d.describle deptdesc FROM emp e LEFT JOIN dept d on(e.deptid=d.id);
SELECT e.*,d.name deptname, d.describle deptdesc FROM emp e RIGHT OUTER JOIN dept d on(e.deptid=d.id);
# 自然连接 真正的开发中用不到
SELECT * from emp natural join dept;
# 自连接
# 查询用户id为1的部门名称
SELECT name
from dept
WHERE id = (SELECT deptId from emp where id=8);
SELECT d.name
from emp e ,dept d
WHERE e.deptid=d.id and e.id = 1
SELECT d.name
from emp e INNER JOIN dept d on(e.deptid=d.id)
where e.id = 1;
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('1', '张三', '男', '110', '郑州', '110', '18', '1');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('2', '李四', '女', '110', '郑州', '120', '18', '2');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('3', '王五', '男', '130', '杭州', '130', '20', '3');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('4', '赵六', '女', '140', '广州', '140', '23', '4');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('5', '钱七', '女', '15000', '兰州', '150', '33', '5');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('6', '王八', '女', '16', '徐州', '180', '38', '6');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('7', '老牟', '男', '110120', '兰州', '110', '33', '7');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('8', '老李', '男', '200000', '郑州', '205845', '38', '8');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('9', '老增', '女', '2356', '长沙', '256789', '29', '3');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('10', 'xx', '女', '123456', '郑州', '123456', '21', '2');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('11', '刘xxx', '男', '456123', '郑州', '78952', '23', '3');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('12', 'xxxx', '女', '12548', '郑州', '458123', '21', '4');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('13', 'xx', '女', '1254', '郑州', '25561', '25', '4');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('14', 'xx', '女', '125', '郑州', '1459', '25', '2');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('15', 'xxx', '女', '45612', '郑州', '75251', '29', '5');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('16', 'xx', '女', '110', '北京', '154', '27', '6');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('17', 'xxx', '女', '152', '铁岭', '1554', '21', '7');
INSERT INTO `db_py1805`.`emp` (`id`, `name`, `gender`, `tel`, `address`, `qq`, `age`, `deptId`) VALUES ('18', '佚名', '未知', '110', '东北', '255', '18', '100');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('1', '总经理班', '管人的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('2', '研发部', '开发软件的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('3', '销售部', '卖东西的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('4', '市场部', '调研的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('5', '人事部', '人力资源管理');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('6', '后勤部', '扫地的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('7', '财务部', '发钱的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('8', '保安部', '打人的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('9', '公关部', '你懂得');