数据库概论:
目录
数据库概论:
数据库
连接数据库软件
数据库操作:
库相关的SQL
数据库相关SQL 回顾:
表相关SQL
表相关的SQL回顾:
数据相关SQL
数据相关SQL回顾:
注:基础已经结束,进阶篇:详细查询
数据库
- 学习数据库主要学习的就是如何对数据进行增删改查.
- DBMS: DatabaseManagementSystem,数据库管理系统(数据库软件),DBMS负责管理数据库文件, 负责将数据保存到数据库文件中.
- 常见的DBMS有:
- MySQL: Oracle公司产品, MariaDB其实就是MySQL的一个分支 , MySQL市占率排名第一
- Oracle: Oracle公司产品, 闭源, 性能最强价格最贵. 市占率第二
- SQLServer: 微软公司产品,闭源 , 市占率第三
- DB2: IBM公司产品,闭源
- SQLite: 轻量级数据库, 安装包只有几十k , 只具备最基本的增删改查功能
连接数据库软件
-
windows: 开始菜单-MySQL->MySQL->输入密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
这是密码错误的提示
-
linux/mac os: 桌面空白处右键->打开终端-> 输入:
mysql -uroot -p 回车 输入密码 再回车
- 先建库再建表, 数据是保存在表当中的, 数据是保存在表中的, 而表是存在于数据库中.
- 数据库文件类似于Excel文件
- MySQL: Oracle公司产品, MariaDB其实就是MySQL的一个分支 , MySQL市占率排名第一
- Oracle: Oracle公司产品, 闭源, 性能最强价格最贵. 市占率第二
- SQLServer: 微软公司产品,闭源 , 市占率第三
- DB2: IBM公司产品,闭源
- SQLite: 轻量级数据库, 安装包只有几十k , 只具备最基本的增删改查功能
-
windows: 开始菜单-MySQL->MySQL->输入密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 这是密码错误的提示
-
linux/mac os: 桌面空白处右键->打开终端-> 输入:
mysql -uroot -p 回车 输入密码 再回车
- 先建库再建表, 数据是保存在表当中的, 数据是保存在表中的, 而表是存在于数据库中.
- 数据库文件类似于Excel文件
数据库操作:
库相关的SQL
-
查询所有数据库
- 格式: show databases;
-
创建数据库
-
默认字符集格式: create database 数据库名;
-
指定字符集格式: create database 数据库名 charset=utf8/gbk;
-
举例:
create database db1; create database db2 charset=utf8; create database db3 charset=gbk; show databases;
-
-
查询数据库信息
-
格式: show create database 数据库名;
-
举例:
show create database db1; show create database db2; show create database db3;
-
-
删除数据库
-
格式: drop database 数据库名;
-
举例:
drop database db2; drop database db3;
-
-
使用数据库
-
操作表相关的SQL语句或数据相关的SQL语句之前必须使用了某一个数据库
-
格式: use 数据库名;
use db1;
-
-
创建 mydb1和mydb2 数据库 字符集分别为utf8和gbk
create database mydb1 charset=utf8; create database mydb2 charset=gbk;
-
查询所有数据库检查是否创建成功
show databases;
-
检查两个数据库的字符集是否正确
show create database mydb1; show create database mydb2;
-
先使用mydb2 再使用 mydb1
use mydb1; use mydb2;
-
删除两个数据库
drop database mydb1; drop database mydb2;
-
必须使用了某个数据库之后再执行表相关的SQL
use db1;
数据库相关SQL 回顾:
- 查询所有: show databases;
- 创建: create database db1 charset=utf8/gbk;
- 查询信息: show create database db1;
- 删除: drop database db1;
- 使用: use db1;
表相关SQL
-
创建表
-
格式: create table 表名(字段1名 类型,字段2名 类型)charset=utf8/gbk;
-
举例:
create table person(name varchar(50),age int)charset=utf8;
创建学生表student 字段:id int,名字,chinese int,math int, english int 字符集gbk
create table student(id int,name varchar(50),chinese int,math int,english int)charset=gbk;
-
查询所有表
- 格式: show tables;
-
查看表信息
-
格式: show create table 表名;
show create table person;
-
查看表字段
-
格式: desc 表名;
desc student;
-
删除表
-
格式: drop table 表名;
drop table student;
-
修改表名
-
格式: rename table 原名 to 新名;
rename table person to per;
-
添加表字段
use db1;
create table emp(name varchar(30));
alter table emp add age int;
alter table emp add id int first;
alter table emp add gender varchar(1) after name;
-
最后面添加格式: alter table 表名 add 字段名 类型;
-
最前面添加格式: alter table 表名 add 字段名 类型 first;
-
在xxx字段后面添加格式: alter table 表名 add 字段名 类型 after xxx;
-
举例:
-
删除表字段
-
格式: alter table 表名 drop 字段名;
alter table emp drop age;
-
修改表字段
-
格式: alter table 表名 change 原名 新名 新类型;
alter table emp change gender age int;
-
表相关的SQL回顾:
- 创建表: create table t1 (name varchar(20),age int)charset=utf8/gbk;
- 查询所有: show tables;
- 查询信息: show create table t1;
- 表字段: desc t1;
- 删除表: drop table t1;
- 修改表名: rename table t1 to t2;
- 添加表字段: alter table 表名 add 字段名 类型 first/after xxx;
- 删除表字段: alter table 表名 drop 字段名;
- 修改表字段: alter table 表名 change 原名 新名 新类型;
数据相关SQL
- 插入数据
- 全表插入格式: insert into 表名 values(值1,值2);
-
指定字段插入格式: insert into 表名(字段1名,字段2名) values(值1,值2);
-
批量插入: 在values后面写多组值通过逗号分割
-
举例:
insert into person values("Tom",18);
insert into person(name) values("Jerry");
insert into person values("LiLei",20),("Hanmeimei",30); -
中文问题:
insert into person values("刘德华",50);
如果执行上面SQL语句提示错误并在错误信息中显示16进制内容执行以下SQL语句
set names gbk;
-
查询数据
举例:
-
select name from person; //所有名字
select name,age from person; //查询所有名字和年龄
select * from person; //查询所有字段信息
select * from person where name="Tom";
select name from person where age<=20;
- 格式: select 字段信息 from 表名 where 条件;
- 修改数据
- 格式: update 表名 set 字段名=值,字段名=值 where 条件;
-
举例:
update person set name="汤姆" where name='Tom';
update person set age=50 where name='Jerry';
-
删除数据
-
举例:
delete from person where name="汤姆";
delete from person where age=50;
delete from person where age<30;
delete from person;
-
格式: delete from 表名 where 条件;
数据相关SQL回顾:
- 插入数据: insert into 表名(字段1名,字段2名) values(值1,值2),(值1,值2);
- 查询数据: select 字段信息 from 表名 where 条件;
- 修改数据: update 表名 set xxx=xxx,xxx=xxx where 条件;
- 删除数据: delete from 表名 where 条件;
创建表
-
格式: create table 表名(字段1名 类型,字段2名 类型)charset=utf8/gbk;
-
举例:
create table person(name varchar(50),age int)charset=utf8; 创建学生表student 字段:id int,名字,chinese int,math int, english int 字符集gbk create table student(id int,name varchar(50),chinese int,math int,english int)charset=gbk;
查询所有表
- 格式: show tables;
查看表信息
-
格式: show create table 表名;
show create table person;
查看表字段
-
格式: desc 表名;
desc student;
删除表
-
格式: drop table 表名;
drop table student;
修改表名
-
格式: rename table 原名 to 新名;
rename table person to per;
添加表字段
use db1; create table emp(name varchar(30)); alter table emp add age int; alter table emp add id int first; alter table emp add gender varchar(1) after name;
-
最后面添加格式: alter table 表名 add 字段名 类型;
-
最前面添加格式: alter table 表名 add 字段名 类型 first;
-
在xxx字段后面添加格式: alter table 表名 add 字段名 类型 after xxx;
-
举例:
删除表字段
-
格式: alter table 表名 drop 字段名;
alter table emp drop age;
修改表字段
-
格式: alter table 表名 change 原名 新名 新类型;
alter table emp change gender age int;
-
表相关的SQL回顾:
- 创建表: create table t1 (name varchar(20),age int)charset=utf8/gbk;
- 查询所有: show tables;
- 查询信息: show create table t1;
- 表字段: desc t1;
- 删除表: drop table t1;
- 修改表名: rename table t1 to t2;
- 添加表字段: alter table 表名 add 字段名 类型 first/after xxx;
- 删除表字段: alter table 表名 drop 字段名;
- 修改表字段: alter table 表名 change 原名 新名 新类型;
- 插入数据
- 全表插入格式: insert into 表名 values(值1,值2);
-
指定字段插入格式: insert into 表名(字段1名,字段2名) values(值1,值2);
-
批量插入: 在values后面写多组值通过逗号分割
-
举例:
insert into person values("Tom",18); insert into person(name) values("Jerry"); insert into person values("LiLei",20),("Hanmeimei",30); -
中文问题:
insert into person values("刘德华",50);
如果执行上面SQL语句提示错误并在错误信息中显示16进制内容执行以下SQL语句
set names gbk;
-
查询数据
举例:-
select name from person; //所有名字 select name,age from person; //查询所有名字和年龄 select * from person; //查询所有字段信息 select * from person where name="Tom"; select name from person where age<=20;
- 格式: select 字段信息 from 表名 where 条件;
-
- 修改数据
- 格式: update 表名 set 字段名=值,字段名=值 where 条件;
-
举例:
update person set name="汤姆" where name='Tom'; update person set age=50 where name='Jerry';
-
- 格式: update 表名 set 字段名=值,字段名=值 where 条件;
-
删除数据
-
举例:
delete from person where name="汤姆"; delete from person where age=50; delete from person where age<30; delete from person;
-
格式: delete from 表名 where 条件;
-
数据相关SQL回顾:
- 插入数据: insert into 表名(字段1名,字段2名) values(值1,值2),(值1,值2);
- 查询数据: select 字段信息 from 表名 where 条件;
- 修改数据: update 表名 set xxx=xxx,xxx=xxx where 条件;
- 删除数据: delete from 表名 where 条件;



