mysql从入门到精通(1)

###数据库
之前通过流去操作文件保存数据的弊端:
1. 执行效率低
2. 开发成本高
3. 一般只能保存小量数据
4. 只能保存文本数据

####什么是DB
-  DataBase 数据库: 代表文件集合 
####什么是DBMS
- DataBaseManagementSystem 数据库管理系统(软件) ,用于管理保存数据的文件集合,用于和程序员进行交互,常见的DBMS有:Oracle MySQL DB2 SQLServer Sqlite ,DBMS具备网络访问能力

####SQL
- Structured Query Language:结构化查询语言,用户程序员和DBMS进行交互,用于程序员告诉DBMS到底对数据进行什么操作的  

###数据库的分类(了解)
- 关系型数据库:经过数学理论验证可以将现实生活中的各种关系保存到数据库,这种就称为关系型数据库。保存数据以表为单位
- 非关系数据库:一般都是为了解决某些特定场景的问题比如:缓存,高并发访问,Redis数据库(以key-value形式保存数据)
####常见的关系型数据
- MySQL: 属于Oracle公司的产品,08被Sun公司收购,09年Sun公司被Oracle收购,开源免费,被收购后发布5.5版本使用Oracle的部分技术,性能提高了30%以上,用户量增多,计划把MySQL闭源 原MySQL程序员离开Oracle创建了MariaDB 老板女儿Maria。市场排名第一
- Oracle: 排名第二,闭源 性能最高 收费最贵
- DB2: IBM公司产品 闭源项目
- SQLServer:微软公司产品  闭源
- Sqlite:轻量级数据库,安装包几十k,应用在嵌入式设备或移动设备上,

###开源和闭源
- 开源:开放源代码 免费试用,通过卖服务盈利,社会上会有一些大牛程序员会无偿的维护和升级 

- 闭源:不开放源代码 收费,通过卖产品+服务盈利 ,有大牛会攻击破坏,但是人家养了一群人维护升级
###打开客户端连接MySQL
- 在终端中执行: mysql -uroot -p 回车  如果有密码写密码 回车 如果没有密码则直接回车

######: 介绍一个操作数据库的可视化工具:Navicat,
######:    Navicat快捷键:
            1.ctrl+q           打开查询窗口
            2.ctrl+/           注释sql语句
            3.ctrl+shift +/  解除注释
            4.ctrl+r           运行当前查询窗口所有sql语句
            5.ctrl+shift+r       只运行选中的sql语句
            7.ctrl+l            删除一行
            8.ctrl+n           打开一个新的查询窗口
            9.ctrl+w          关闭一个查询窗口

###和数据库相关的SQL
####查询所有数据库
- show databases;
####创建数据库
- create database db1;
####查看数据库详情
- show create database db1;
####创建数据库指定字符集
- create database db2 character set gbk/utf8;
####删除数据库
- drop database db2;
#####练习: 创建db2 db3 db4 db5  里面3,4,5字符集为gbk  最后删除每一个
####使用数据库
- use db1;

查询所有: show databases 
创建 : create database db1;
查询单个 show create database db1;
指定字符集 create database db1 character set gbk/utf8;
删除  drop database db1;
使用  use db1;
###和表相关的SQL
####查询所有表
- show tables;
####创建表
- create table 表名(字段1名 字段1的类型,字段2名 字段2的类型,.....);
    create table person(name varchar(10),age int);
-创建一个学生表(student) 保存学号id,姓名name,年龄age,语文chinese,数学math,英语english 
    create table student(id int,name varchar(10),age int,chinese int,math int,english int);
####查看表详情
- show create table 表名;
    show create table person;
####创建表时指定表的引擎和字符集
- create table t1(name varchar(10)) engine=myisam charset=gbk;


####表的引擎
- innodb:支持数据库的高级操作如:外键、事务等,默认引擎
- myisam:只支持基础的增删改查操作
     
###SQL格式:
1. 可以有换行 
2. 最后以;结尾
3. 关键字之间需要有空格(可以写多个空格,建议写一个)


总结:
查询所有  
show databases;
创建  create database db1;
查询详情   show create database db1;
指定字符集 create database db1 character set gbk/utf8;
删除 drop database db1;
使用 use db1;

表相关
查询所有
show tables;
创建  create table t1(name varchar(10),age int);
查询详情 show create table t1;
指定引擎和字符集: create table t1(name varchar(10),age int) engine=myisam/innodb charset=gbk/utf8;

 
####查看表字段
- desc 表名;
####删除表
- drop table 表名;
####修改表相关
1. 修改表名
- rename table 原名 to 新名;
    rename table student to stu;
2. 修改表的引擎和字符集
- alter table 表名 engine=myisam/innodb charset=utf8/gbk;
    alter table stu engine=myisam charset=gbk;
3. 添加表字段
- 最后面: alter table 表名 add 字段名 字段类型;
- 最前面: alter table 表名 add 字段名 字段类型 first;
- xxx的后面: alter table 表名 add 字段名 字段类型 after xxx;
    create table hero(name varchar(10));
    alter table hero add age int;
    alter table hero add id int first;
    alter table hero add sal int after name;
4. 删除表字段
- alter table 表名 drop 字段名;
    alter table hero drop sal;
5. 修改表字段的名字和类型
- alter table 表名 change 原字段名 新字段名 新字段类型;
    alter table hero change name heroname varchar(5);
6. 修改表字段的类型和位置
- alter table 表名 modify 字段名 类型 位置;
    alter table hero modify age int first(after xxx);


- 创建表 create table t1(name varchar(10)) engine=myisam charset=gbk;
- 查询所有 show tables;
- 表详情 show create table t1;
- 表字段 desc t1;
- 删除表 drop table t1;
-修改表相关
改表名: rename table t1 to t2;
改引擎字符集: alter table t1 engine=myisam/innodb charset=gbk/utf8;
添加字段: alter table t1 add age int first(after xxx);
删除字段: alter table t1 drop age;
修改字段名称和类型: alter table t1 change sal salary int;
修改字段类型和位置: alter table t1 modify age int first(after name);

###练习:
1. 创建数据库newdb并使用, 里面创建员工表t_emp只有name字段 引擎为myisam 字符集为gbk
    create database newdb;
    use newdb;
    create table t_emp(name varchar(10)) engine=myisam charset=gbk;
2. 修改表名为emp
    rename table t_emp to emp;
3. 修改引擎为innodb 字符集为utf8
    alter table emp engine=innodb charset=utf8;
4. 添加部门编号字段deptno 在最后面
    alter table emp add deptno int;
5. 添加员工编号 empno在最前面
    alter table emp add empno int first;
6. 添加salary字段在name的后面
    alter table emp add salary int after name;
7. 修改salary字段名字为sal,把sal放在empno的后面
    alter table emp change salary sal int;
    alter table emp modify sal int after empno;
8. 删除sal字段  alter table emp drop sal;
9. 删除表  drop table emp;
10. 删除数据库 drop database newdb;

###数据相关
####插入数据
    create table emp(id int,name varchar(10),age int,sal int);    
- 全表插入数据:
    - insert into emp values(1,'Tom',18,3000);
- 指定字段插入数据: 
    - insert into emp (name,age) values('Jerry',19);
    - insert into emp (name) values('李白');
- 批量插入数据:
    insert into emp values(3,'刘备',28,6000),(4,'张飞',20,5000),(5,'关羽',25,9000);
    insert into emp (name,age) values('悟空',500),('八戒',400),('沙僧',200);
####查询数据
- 查询全部数据的全部字段信息
    select * from emp;
- 查询所有员工的姓名和年龄
    select name,age from emp;
- 查询年龄在25岁以下的员工信息
    select * from emp where age<25;
- 查询工资3000块钱的员工姓名、年龄、工资
    select name,age,sal from emp where sal=3000;    
####修改数据
- 修改Tom的工资为3333
    update emp set sal=3333 where name='Tom';
- 修改30岁以下的工资为666
    update emp set sal=666 where age<30;
- 修改id等于3的名字为吕布 年龄为55 工资为20000
    update emp set name='吕布', age=55, sal=20000 where id=3;
- 修改工资为null的工资为800
    update emp set sal=800 where sal is null;
####删除数据
- 删除id=1的员工
    delete from emp where id=1;
- 删除年龄在25岁以下的员工
    delete from emp where age<25;
- 删除全部数据
    delete from emp;


insert into t1 (字段名1,字段名2) values(值1,值2),(值1,值2);
select name,age from t1 where id<5;
update t1 set 字段名=值, 字段名=值 where id=5;
delete from t1 where id<3;

###练习:
1. 创建hero表如果存在则先删除再创建, id 姓名name 类型type 金币money
2. 插入以下数据   1 诸葛亮 法师 18888 , 2 孙悟空 打野 18888 ,3 小乔 法师 6888,4 黄忠 射手 8888, 5 刘备 战士 6888
3. 修改所有18888的为28888
4. 修改所有打野为刺客
5. 删除价格为6888的英雄
6. 修改孙悟空为猪八戒
7. 删除id为1,2,3的英雄
8. 修改所有英雄的类型为已阵亡
9. 删除所有数据
10. 删除表

###中文字符问题
- set names gbk;
###总结
####数据库相关
1. 查询所有 
    show databases;
2. 创建
    create database db1 character set utf8/gbk;
3. 查看详情
    show create database db1;
4. 删除 
    drop database db1;
5. 使用
    use db1;
####表相关
1. 创建
    create table t1(name varchar(10),age int) engine=myisam/innodb charset=gbk/utf8;
2. 查询所有
    show tables;
3. 查看表详情
    show create table t1;
4. 查看表字段
    desc t1;
5. 删除表
    drop table t1;
6. 修改表
- 修改表名 rename table t1 to t2;
- 修改引擎 alter table t1 engine=myisam/innodb charset=gbk/utf8;
- 添加字段 alter table t1 add age int first(after xxx);
- 删除字段 alter table t1 drop age;
- 修改名称和类型 alter table t1 change age newAge int;
- 修改类型和位置 alter table t1 modify age int first(after xxx);
####数据相关
1. 插入数据
    insert into 表名 (name,age) values(值1,值2),(值1,值2),(值1,值2);
2. 查询数据
    select name,age from 表名 where id<5;
3. 修改数据
    update 表名 set age=18 where id=5;
4. 删除数据
    delete from 表名 where id=3;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值