目录
DML Data Maniputlation Language数据操作语言 (增删改 查)
DQL Data Query Language 数据查询语言 (查)
数据库
以前通过IO流技术自己对数据进行操作,需要书写大量的程序代码,程序的质量不能保证,而且开发周期很长, 在互联网行业中 任何使用频繁又很复杂的部分,通常都会有人提供一套通用的解决方案,数据库软件就是三方公司提供的对数据进行增删改查操作的方案,作为程序员,咱们只需要学习如何使用数据库软件,要和数据库软件进行交流需要学习一门语言,叫做SQL.
Structured Query Language: 结构化查询语言.
详情可参考百度百科https://baike.baidu.com/item/%E6%95%B0%E6%8D%AE%E5%BA%93/103728?fr=aladdin
DBMS(数据库软件)
Database数据库Managenment管理System系统(俗称数据库软件)
MySQL:
市占率排名第一, Oracle公司产品,08年被Sun公司收购,09年Sun被Oracle收购. 开源软件, MariaDB(前身就是MySQL)
Oracle:
市占率排名第二,Oracle公司产品, 老板 拉里埃里森 32 闭源 性能最高价格最贵
SQLserver:
市占率排名第三,微软产品 闭源
网站完整解决方案: 开发语言+web服务器软件+数据库软件+操作系统
SQLite:
轻量级数据库. 主要应用在移动设备和嵌入式设备中
开源和闭源
- 开源:开发源代码,免费, 盈利方式:靠卖服务赚钱 , 有技术大拿无偿升级维护
- 闭源:不公开源代码,收费,盈利方式:靠卖产品+卖服务, 有技术大拿攻击产品(但是没事儿公司有钱 养着一群人维护升级产品)
DDL(数据库定义语言)
- MySQL数据库服务软件中,需要保存数据的话, 先建库再建表
如何连本地MySQL
1. windows: 开始菜单-> 所有程序->MySQL->MySQL Client ->输入密码 回车
2. linux: 桌面 右键 打开终端->mysql -uroot -p 回车-> 输入密码 回车
- 退出连接: exit;
数据库相关
1. 查询所有数据库
- 格式:show databases;
2.创建数据库
- 格式: create database 数据库名;
- 指定字符集格式: create database 数据库名 character set utf8/gbk;
create database db1;
create database db2 character set gbk;
3. 查看数据库详情
- 格式: show create database 数据库名;
show create database db1;
4. 删除数据库
- 格式: drop database 数据库名;
drop database db2;
drop database db1;
5. 使用数据库
- 格式: use 数据库名;
create database db1;
use db1;
练习:
1. 分别创建mydb1和mydb2数据库 第一个是gbk第二个是utf8
create database mydb1 character set gbk;
create database mydb2 character set utf8;
2. 创建完后查询两个是不是都存在
show databases;
3. 查询两个字符集是否正确
show create database mydb1;
show create database mydb2;
4. 先使用mydb1再使用mydb2
use mydb1;
use mydb2;
5. 删除两个数据库
drop database mydb1;
drop database mydb2;
表相关
注意!!!
切记操作表和数据时一定要保证已经使用了数据库
create database db1;
use db1;
1. 创建表
- 格式: create table 表名(字段1名 字段1类型,字段2名 字段2类型);
create table person(name varchar(10),age int);
- 练习:创建学生表(student) 字段:学号id,姓名name,语文chinese,数学math
create table student(id int,name varchar(10),chinese int,math int);
2. 查询所有表
- 格式: show tables;
3. 查询表详情
- 格式: show create table 表名;
show create table person;
4. 创建表时指定字符集
- 格式: create table 表名(字段1名 字段1类型,字段2名 字段2类型) charset=utf8/gbk;
create table t1(name varchar(10)) charset=gbk;
create table t2(name varchar(10)) charset=utf8;
5. 查询表字段
- 格式: desc 表名;
desc student;
6. 删除表
- 格式: drop table 表名;
drop table t1;
drop table t2;
7. 添加表字段
- 最后添加格式: alter table 表名 add 字段名 类型;
- 最前面添加格式: alter table 表名 add 字段名 类型 first;
- 在xxx字段后面添加: alter table 表名 add 字段名 类型 after xxx;
alter table person add gender varchar(5);
alter table person add id int first;
alter table person add salary int after name;
8. 删除表字段
- 格式: alter table 表名 drop 字段名;
alter table person drop salary;
9. 修改表字段
- 格式: alter table 表名 change 原名 新名 新类型;
alter table person change gender salary int;
数据库和表相关练习:
1. 创建数据库mydb1 字符集为utf8 并使用该数据库
create database mydb1 character set utf8;
use mydb1;
2. 在mydb1中创建员工表emp 字段有name 字符集为utf8
create table emp(name varchar(10))charset=utf8;
3. 添加表字段age在最后面
alter table emp add age int;
4. 添加id字段在最前面
alter table emp add id int first;
5. 添加sal字段在name的后面
alter table emp add sal int after name;
6. 修改字段sal为salary
alter table emp change sal salary int;
7. 删除age字段
alter table emp drop age;
8. 删除emp表
drop table emp;
9. 删除数据库
drop database mydb1;
DML Data Maniputlation Language数据操作语言 (增删改 查)
开始学习之前先建里数据库和表结构
create database mydb1 character set utf8;
use mydb1;
create table person(id int,name varchar(10),age int) charset=utf8;
1. 插入数据
- 全表格式: insert into 表名 values(值1,值2,值3);
insert into person values(1,'Tom',18);
- 指定字段插入格式: insert into 表名(字段1名,字段2名) values(值1,值2);
insert into person(id,name) values(2,'Jerry');
批量插入:
insert into person values(3,'aaa',10),(4,'bbb',11),(5,'ccc',12);
insert into person (id,name) values(6,'ddd'),(7,'eee');
数据库输入出现中文问题:
insert into person values(8,'刘德华',25);
如果执行上面代码出错则使用下面代码后再次执行
set names gbk;
MySQL处理数据有两个地方需要进行编码和解码 每个地方的编解码要保证字符集一致, 网络数据传输有一对编解码, 往磁盘中保存和读取数据也有一对编解码,创建数据库和表时指定的字符集是服务于后者的, set names gbk是服务于前者的.
2. 修改数据
- 格式: update 表名 set 字段名=xxx,字段名=xxx where 条件;
update person set age=28 where name='Tom';
update person set age=80 where id>5;
3. 删除数据
- 格式: delete from 表名 where 条件;
delete from person where id=5;
delete from person where age=80;
delete from person;
DQL Data Query Language 数据查询语言 (查)
- 格式: select 字段信息 from 表名 where 条件;
select name from person; //查询表中所有的名字
select name,age from person where id<5; //查询id小于5的名字和年龄
select * from person where id>=5; //查询id大于等于5的所有信息
练习:
1. 创建hero表 id 名字name 类型type 价格money 字符集为utf8
create table hero(id int,name varchar(10),type varchar(10),money int)charset=utf8;
2. 保存以下数据:
insert into hero values
(1, '诸葛亮', '法师', 18888),(2, '周瑜', '法师', 13888),
(3, '孙悟空', '打野', 18888),(4, '小乔', '法师', 13888),
(5, '黄忠', '射手', 8888) ,(6, '刘备', '战士', 6888);
3. 修改所有18888为28888
update hero set money=28888 where money=18888;
4. 修改所有法师为战士
update hero set type='战士' where type='法师';
5. 删除价格为6888的英雄
delete from hero where money=6888;
6. 修改小乔为猪八戒
update hero set name='猪八戒' where name='小乔';
7. 删除价格低于15000的英雄
delete from hero where money<15000;
8. 添加性别gender字段在name的后面
alter table hero add gender varchar(5) after name;
9. 修改所有英雄的性别为男
update hero set gender='男';
10. 删除所有数据
delete from hero;
11. 删除表
drop table hero;