MySql
数据库简介
因为文本形式存储数据存在很多弊端:
1.执行效率低
2.占用内存
什么是DB
DataBase :数据库
数据库本质就是文件集(多个文件)
会按照特定存储规范进行数据增删改查
什么是DBMS
DataBaseManagementSystem:数据库管理系统
本质就是管理数据库文件的软件
Oracle Mysql DB2 Sqlite Sqlserver
数据库分类
-
关系型数据库
是经过数学理论验证过 可以保存现实生活中任何关系的数据库 存放数据是以表为单位 Oracle Mysql DB2 Sqlite Sqlserver都是关系型数据库
-
非关系型数据库
Redis数据库(非关系型数据库) Key-Value
开源和闭源
开源:免费 开发源代码 大牛维护
闭源:收费 不公开源代码 大牛攻击
主流关系型数据库介绍
-Mysql : 开源 卖服务赚钱 被Sun收购 Sun被Oracle收购
5.5版本因为Oracle技术大牛对其进行升级 性能大幅提升,Mysql从6.0开始收费,导致原mysql工程师离职创建了MariaDB MariaDB内部实际上就是mysql,创建者的女儿叫Maria
-支持操作系统:Windows Linux
Oracle:
闭源 卖产品,性能最高,价格最贵,ceo 拉里埃里森 32岁前一无所有 20年后硅谷首付
-支持操作系统:Windows Linux
DB2:
闭源 卖产品,大型数据库,通常国字头的企业在用,
-支持操作系统: Linux unix
sqlite:
轻量级数据库,大小只有几十k,应用于移动或嵌入式设备
SQLServer
微软公司 闭源 主要用于 .net开发的网站中
安装MySql数据库
注意两点: 端口号3306 编码utf-8
SQL
什么是SQL
Strctured Query Language :结构化查询语言
通过sql语言和DBMS(数据库软件) 进行交互
连接数据库
终端中 输入: mysql -uroot -p 回车
然后输入密码 再回车 如果没有密码 直接回车
数据库和表的关系
数据库相关SQL
查看所有数据库
show databases;
创建数据库
格式:create database 数据库名称;
例: create database db1;
查看单个数据库
格式:show create database db1;
创建数据库指定字符集
格式: create database db2 character set utf8;
删除数据库
drop database db2;
使用数据库
use 数据库名;
SQL 语法格式
- sql不区分大小写
- 每行sql以;结尾
- 指令之间用空格分割 (可以一个也可以多个,通常一个),指令之间也可以有一个或多个换行符
表相关SQL
什么是表
表是数据库中存放数据的单元,任何数据都是存放到表中
类似于java中的Class,表中的字段对应Class的属性
创建表
格式: create table 表名(字段1名 类型,字段2名 类型,字段3名 类型);
例: create table person(name varchar(10),age int);
执行原理:当在终端中输入完建表语句敲回车 终端会把写好的sql 发送给DBMS(mysql),然后解析到create table 时 识别出要创建一个表, 在表名后面的括号内部的内容是决定此表有哪些字段
查询所有表
格式:show tables;
查看表属性(引擎和字符集)
查询单个表的格式:show create table person;
查看表字段详情
格式:desc 表名;
`和’ 的区别
`使用在表名或字段名上 (可省略)
' 当使用字符串时 使用
数据库表的引擎
-innoDB:默认,支持数据库的高级操作,包括事务 主 外键等
-myisam: 只具备基本的数据存储功能
创建表时指定引擎和字符集
格式: create table t1(id int,name varchar(10)) engine=myisam charset=utf8;
示例
1. 创建字符集为utf8的数据库db5并使用
create database db5 character set utf8;
use db5;
2. 创建student表,引擎为myisam 字符集为utf8,字段有id int 名字 字符串和年龄int
create table student(id int,name varchar(10),age int) engine=myisam charset=utf8;
对已经创建好的表进行修改
-
修改表名称
-格式: rename table 原名 to 新名; rename table student to t_stu;
-
修改表的属性 引擎和字符集
-格式: alter table 表名 engine=innodb charset=utf8; alter table t_stu engine=innodb charset=utf8;
-
添加表字段
-在最后添加 格式:alter table 表名 add fatherAge int; -在最前添加 格式:alter table 表名 add fatherAge int first; -在某个字段后面添加 格式:alter table 表名 add fatherAge int after id;
-
修改字段名和类型
-格式:alter table 表名 change 原字段名 新字段名 新类型; 例如: alter table t_stu change age money int;
-
修改字段类型和位置
-格式:alter table 表名 modify 字段名 类型 位置(first/after ***); 例如: alter table t_stu modify money int first;
-
删除字段
-格式:alter table 表名 drop 字段名; 例如: alter table t_stu drop oldAge;
1.改名 rename table t_stu to student;
alter table t_stu engine=myisam charset=gbk
alter table t_stu add age int after id;
alter table t_stu change age newAge int
alter table t_stu modify age int first;
alter table t_stu drop age;
示例
-
创建t_hero表引擎为myisam 字段为 id 和 name
create table t_hero(id int,name varchar(10)) engine=myisam;
-
修改表名为 hero
rename table t_hero to hero;
-
修改引擎为innodb
alter table hero engine=innodb;
-
在 id后面添加age字段类型为varchar(10)
alter table hero add age varchar(10) after id;
-
修改age字段名称为myAge 类型为int
alter table hero change age myAge int;
-
修改myAge 字段 放在最后面
alter table hero modify myAge int after name;
-
删除myAge字段
alter table hero drop myAge;
删除表
drop table t_stu;
总结和表相关的操作:
查看所有表
创建表 并且指定引擎和字符集
查看单个表属性
查看表字段 desc
修改表名
修改属性
添加字段
修改字段名和类型
修改字段类型和位置
删除字段
删除表
数据相关SQL
数据库要求utf8 创建hero表 utf8
字段 id int name varchar(10) age int
create table hero(id int,name varchar(10),age int);
-
插入语句
-全表插入格式: insert into 表名 values(值1,值2,值3); 格式要求:values里面的值的数量和顺序要和表的字段一致 例如: insert into hero values(1,'悟空',502); -指定字段插入格式: insert into 表名 (字段1名,字段2名) values(值1,值2) 例如: insert into hero (name) values ('八戒'); -批量插入: insert into hero values(11,'美国队长',34),(12,'钢铁侠',43),(13,'蜘蛛侠',23),(14,'雷神',55); -指定字段的批量插入 思考:插入张三 李四 王五 三个人名 insert into hero (name) values('张三'),('李四'),('王五');
-
查询语句
select * from hero; *代表所有字段 -只查询所有名字 select name from hero; -查询名字和年龄 select name,age from hero;
-
更新语句(修改)
-格式: update 表名 set age=25 where id=1; **不加where条件则对整表数据操作** 例子:update hero set age=25 where id=1;
-
删除语句
-格式: delete from 表名 where 条件; -如果不带条件 则删除整表数据 留下空表 例子:delete from hero where name='李四';
-
TRUNCATE语句
truncate table hero;//先删除表再创建一个一样的空表 delete from hero;//只是删除表里面的数据 drop table hero;//删除表
练习:
1. 创建员工表(emp) 字段id name age salary(工资)
create table emp(id int,name varchar(10),age int,salary int);
2. 插入一下几条数据:
insert into emp values
(1,'刘德华',22,8000),
(2,'张学友',21,8000),
(3,'赵四儿',55,3000),
(4,'奥巴马',33,10000);
3. 修改工资为8000的年龄为18岁
update emp set age=18 where salary=8000;
4. 修改55岁的工资为800
update emp set salary=800 where age=55;
5. 删除工资为800的
delete from emp where salary=800;
6. 修改奥巴马为特朗普
update emp set name='特朗普' where id=4;
7. 修改年龄小于30岁的工资为8888;
update emp set salary=8888 where age<30;
8. 把id为2的名字改成黎明 年龄改成23;
update emp set name='黎明',age=23 where id=2;
总结:
数据库相关sql:
查看所有数据库
show databases;
查看单个数据
show create database db1;
创建数据库
create database db1 character set utf8;
删除数据库
drop database db1;
使用数据库
use db1;
表相关sql:
查看所有表
show tables;
创建表
create table person(id int,name varchar(10)) engine=myisam charset=utf8;
查看表属性
show create table person;
查看表字段
desc person;
修改表
改表名
rename table person to t_p;
表属性
alter table person engine=innodb charset=gbk;
添加字段
alter table person add age int (first/after xxx);
修改字段名和类型
alter table person change age newAge int;
修改类型和位置
alter table person modify newAge int (first/after xxx);
删除字段
alter table person drop newAge;
删除表
drop table name;
数据相关sql:
插入数据全表
insert into person values(值1,值2,值3);
插入数据指定字段
insert into person (name,age) values (值1,值2)
查询语句
select * from person;
select name,age from person;
修改
update person set age=22 where id=18;
删除
delete from person where id=5;
trancate 删除 表 然后创建新表
练习1:
- 创建员工表emp
- 创建部门表dept;
练习2:
-
给dept表插入4条数据
10,‘ACCOUNTING’,‘NEW YORK’ --财务部 纽约
20,‘RESEARCH’,‘DALLAS’ --研究部 达拉斯市
30,‘SALES’,‘CHICAGO’ --销售部 芝加哥
40,‘OPERATIONS’,‘BOSTON’ --运营部 波士顿
练习3:
- 创建 mydb 数据库,使用 utf8 编码
- 创建 t_item 商品表
- 在 t_item 表插入商品数据
- 修改 id 是7的商品,修改库存量为20
- 删除商品 7
eclipse SQL客户端
乱码问题
character-set-server=utf8
查看所有数据库
show databases; 唯一带s
创建数据库
create database db1;
指定字符集
create database db2 character set utf8;
查看单个数据库
show create database db1;
删除数据库
drop database db2;
使用数据库
use db1;
创建表
create table t1 (id int,name varchar(20),age int);
查看所有表
show tables;
查看单个
show create table t1;