mysql
顯示當(dāng)前所有的數(shù)據(jù)庫(kù)
show databases
?支持模糊查詢、_表示占位符蜕猫,加反斜杠轉(zhuǎn)義
show databases like "db%";?
show databases like "db/_%";
顯示當(dāng)前所有的數(shù)據(jù)庫(kù)
use dbName;
?顯示當(dāng)前數(shù)據(jù)庫(kù)所有表寂曹、支持模糊查詢
show tables like "t%"
描述表的結(jié)構(gòu)
desc\describe tableName;
描述表的結(jié)構(gòu)
show columns from t_user;
顯示當(dāng)前所在的數(shù)據(jù)庫(kù)
select database();
返回當(dāng)前時(shí)間
select now();
返回當(dāng)前數(shù)據(jù)庫(kù)的版本信息
select version();
創(chuàng)建數(shù)據(jù)庫(kù):
創(chuàng)建數(shù)據(jù)庫(kù):
create database dbName [[default] charset 'utf8']
刪除數(shù)據(jù)庫(kù):
drop database dbName;
修改數(shù)據(jù)庫(kù):
alter database dbName default charset gbk;
查看數(shù)據(jù)庫(kù)定義:
show create database dbName;
表的常見(jiàn)操作
表的創(chuàng)建:
create table tName(
field1 int primary key auto_increment,
gender varchar(20) not null
) default charset utf8 engine=innodb/mysiam
查詢表創(chuàng)建的信息:
show create table tname[\G];
對(duì)表結(jié)構(gòu)的修改:
修改表名:
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
增加一個(gè)新的字段:
alter table tName add newColumn condition 位置
alter table t_test add nickname varchar(50) not null unique
修改一個(gè)字段的名稱
alter table tName change nickname test varchar(30) default 'liushuaige' first;
修改一個(gè)字段的類型
alter table tName modify test varchar(80) default 'hehe' after age;
刪除一個(gè)字段
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、單表的條件查詢
關(guān)系運(yùn)算符: > <? >= <=? =? != <>
邏輯運(yùn)算符: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]
分頁(yè):
limit offset, pageSize;
11回右、編碼問(wèn)題
utf8
gbk
latin1
……
校對(duì)集:
_bin: binary 區(qū)分大小寫
_cs: 區(qū)分大小寫
_ci: 忽略大小寫
=============================================================
今天目標(biāo):
分表:
多表關(guān)聯(lián)查詢:
外鍵:
視圖:
索引:
面試題:
1隆圆、說(shuō)說(shuō)你所知道的mysql的數(shù)據(jù)庫(kù)引擎有哪些?
2翔烁、innodb渺氧、myisam 兩個(gè)引擎的區(qū)別?
=============================================================
在設(shè)計(jì)數(shù)據(jù)庫(kù)時(shí)蹬屹,一般而言要求遵循數(shù)據(jù)庫(kù)三范式(3NF)
1侣背、設(shè)計(jì)的字段具有原子性(字段不可分割)
2白华、每天記錄具有唯一性? 主鍵
3、外鍵
大量數(shù)據(jù)冗余
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;
# 笛卡爾積現(xiàn)象
# 內(nèi)連接
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);
# 自然連接 真正的開(kāi)發(fā)中用不到
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', '長(zhǎng)沙', '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', '總經(jīng)理班', '管人的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('2', '研發(fā)部', '開(kāi)發(fā)軟件的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('3', '銷售部', '賣東西的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('4', '市場(chǎng)部', '調(diào)研的');
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', '財(cái)務(wù)部', '發(fā)錢的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('8', '保安部', '打人的');
INSERT INTO `db_py1805`.`dept` (`id`, `name`, `describle`) VALUES ('9', '公關(guān)部', '你懂得');