MySQL学习

数据库相关知识

数据库的分类

  1. 关系型数据库:经过数学理论验证,可以将现实生活中存在的各种关系,保存到数据库中,这种数据库 称为关系型数据库,在此数据库中,以表的形式保存数据之间的关系。
  2. 非关系型数据库: 主要为了解决特定的应用场景,如:缓存,高并发访问等,存储数据的方式有多种, redis是常见的非关系型数据库,redis是以键值对的形式保存数据。

SQL语言规范

  1. 以;(分号)结尾
  2. 关键字之间有空格,通常只有一个,但多个也可以
  3. 可以存在换行
  4. 数据库名称和表名称区分大小写

连接数据库

1
2
3
mysql -h localhost -P 3306 -u root -p
或者
mysql -uroot -p

退出指令

1
exit

SQL语句

数据库相关SQL语句

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
1. 查看所有数据库
show databases;
2. 创建数据库
	create database 数据库名称;
	create database db1;
	指定字符集:
    create database 数据库名称 character set gbk;
	create database db2 character set gbk;
3. 查看指定数据库详情
	show create database 数据库名称;
	how create database db1;
4. 删除数据库
	drop database 数据库名称;
	drop database db2;
5. 使用数据库
	use 数据库名称;
	use db1;

和表相关的SQL语句

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
1. 创建表
	-格式: create table 表名(字段1名 字段1类型, 字段2名 字段2类型,.....);
	create table stu(id int,name varchar(10),age int, chinese int, math int,english int);	
2. 查询所有表
	show tables;
3. 查看单个表
	show create table t1;
	desc t1;
4. 删除表
	drop table t1;
5. 修改表名称
	rename table t1 to t2;
6. 修改表引擎和字符集
	alter table t1 engine=myisam/innodb charset=gbk/utf8
7. 添加字段
	alter table t1 add age int;
8. 删除字段
	alter table t1 drop age;
9. 修改字段名和类型
	alter table t1 change age myAge int;
10. 修改类型和位置
	alter table t1 modify age int first/after xxx;

与数据相关

1
2
3
4
5
6
7
8
9
1. 插入数据
	insert into t1 (a,b,c) values(1,2,3),(1,2,3);
2. 查询数据
	select * from t1;
	select name,age from t1;
3. 修改数据
	update t1 set age=20 where id=1;
4. 删除数据
	delete from t1 where id=1;

牛刀小试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
1. 创建英雄表hero  字段(id name type sal)
	create table hero(id int,name varchar(10),type varchar(10),sal int);
2. 查看所有表
	show tables;
3. 查看指定表详情 和 表的字段信息
	-格式:show create table 表名;
	-格式:desc 表名;
4. 创建表指定引擎和字符集
	create table t1(id int,name varchar(10)) engine=myisam charset=gbk;
5. 删除表
	drop table t1;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
1. 创建员工表(emp) ,字段有员工编号(empno),员工姓名(ename),员工工资(sal)
	create table emp(empno int, ename varchar(10),sal int);
2. 创建数据库newdb 在newdb中 创建 商品表(item) 字段有商品标题(title),价格(price),库存(num)
	create database newdb;
	use newdb;
	create table item(title varchar(10),price int,num int);
3. 查看员工表详情和字段信息
	use db1;
	show create table emp;
	desc emp;
4. 删除商品表
	use newdb;
	drop table item;

表的引擎

  1. innodb:支持数据库的高级操作,包括:事务,外键

  2. myisam:仅支持数据的增删改查

表的修改

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use db1;
create table person(id int,name varchar(10));
1. 修改表的名称
	-格式: rename table 原名 to 新名;
	rename table person to t_person;
2. 修改表的引擎和字符集
	-格式: alter table 表名 engine=myisam charset=gbk;
	alter table t_person engine=myisam charset=gbk;
	show create table t_person;
3. 添加表的字段
	-最后面格式: alter table 表名 add 字段名 字段类型;
		alter table t_person add age int;
		desc t_person;
	-最前面格式: alter table 表名 add 字段名 字段类型 first;
		alter table t_person add chinese int first;
	-某个字段的后面格式: alter table 表名 add 字段名 字段类型 after 字段名;	
		alter table t_person add math int after id;
4. 删除字段
	-格式:alter table 表名 drop 字段名;
		alter table t_person drop chinese;
5. 修改字段名称和类型
	-格式:alter table 表名 change 原字段名 新字段名 字段类型
	alter table t_person change age myage int;
6. 修改字段的类型和位置
	-格式:alter table 表名 modify 字段名 字段类型 first/after xxx;
	alter table t_person modify myage int after id;

牛刀小试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
1. 创建t_hero表 字段 id name 引擎myisam
	create table t_hero(id int,name varchar(10)) engine=myisam;
2. 修改表名为t_h
	rename table t_hero to t_h;
3. 修改表的引擎为innodb
	alter table t_h engine=innodb;
4. 添加age字段在id的后面
	alter table t_h add age int after id;
5. 删除id字段
	alter table t_h drop id;

数据相关的SQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
create table t_stu(id int,name varchar(10),age int);
1. 插入数据
	-全表插入:要求插入的数据的数量和顺序要和表字段的数量顺序一致,格式:insert into 表名 values(值1,值2,值3....);
		insert into t_stu values(1,'zhangsan',23);
	-指定字段插入格式:insert into 表名 (字段1,字段2...) values(值1,值2...);
		insert into t_stu (id,name) values(2,'lisi');
	-批量插入:
		insert into t_stu values(3,'悟空',23),(4,'八戒',20),(5,'沙僧',18);
		insert into t_stu (id,name) values(6,'刘备'),(7,'关羽'),(8,'貂蝉');
2. 查询数据
	select * from t_stu;
	select name,age from t_stu;

3. 删除数据
	delete from t_stu where name='八戒';
	delete from t_stu where age is null;
4. 修改数据
	update t_stu set name='张三' where id=1;
	update t_stu set name='卷帘大将',age=200 where id=5;

总结

数据库相关

1
2
3
4
5
create database db1 character set utf8;
show databases;
show create database db1;
drop database db1;
use db1;

表相关

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
create table t1 (id int,name varchar(10));
show tables;
show create table t1;
desc t1;
drop table t1;
rename table t1 to t2;
alter table t1 engine=myisam charset=gbk;
alter table t1 add age int first/after xxx;
alter table t1 drop age;
alter table t1 change age myage int;
alter table t1 modify myage int first/after xxx;

数据相关

1
2
3
4
5
6
insert into t1 (id,name,age) values(a,b,c),(a2,b2,c2);
select id,name from t1;
update t1 set a=1,b=2 where id=1;
delete from t1 where id=1;
ange age myage int;
alter table t1 modify myage int first/after xxx;

MySQL学习入门

主键