一、MySQL介绍
二、MySQL安装
1. linux安装mysql
使用yum包进行安装 yum install mysql-server mysql -y
2. windows安装mysql
在mysql官网上下载windows版本的mysql(MySQL Community Server)安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 下载完毕后需要添加mysql的环境变量 a) 初始化:
mysqld --initialise-insecure (--user=mysql)
b) 启动服务端:
mysqld
c) 连接客户端:
mysql -u root -p
d) 创建mysql数据库:
create database sjingx;
e) 制作MySQL的windows服务:
mysql install(创建mysql服务)
mysql remove(删除mysql服务)
f) 创建好MySQL服务的开启与关闭
net start mysql(开启mysql服务)
net stop mysql(关闭mysql服务)
3. OS X安装mysql
4. 基本管理
a) 创建用户 b) 授权 c) 数据库的导入和导出
mysqldump -u 用户名 -p 密码 数据库名称 > 导出文件路径
三、库操作
1. 系统数据库
information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息、列信息、权限信息、字符信息等 performance_schema: MySQL 5.5开始新增一个数据库:主要用于收集数据库服务器性能参数,记录处理查询请求时发生的各种事件、锁等现象 mysql: 授权库,主要存储系统用户的权限信息 test: MySQL数据库系统自动创建的测试数据库
2. 创建数据库
语法: create database 数据库名 charset utf8;
3. 数据库相关操作
create database db1 charset utf8;(创建数据库db1,编码格式为UTF8) show create database sjingx;(查看数据库sjingx信息) show databases;(查看所有数据库) alter database db1 charset gbk;(修改数据db1的编码格式为GBK) drop database db1;(删除数据库)
四、表操作
1. 存储引擎
2. 表的增删改查
1)创建表 语法:
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
#注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的
示例:
txt
create table t1(
id int,
name varchar(50),
sex enum('male','female'),
age int(3)
);
# 查看db1库下所有表名
show tables;
select id,name,sex,age from t1;
select * from t1;
select * from t1;
# 往表中插入数据
insert into t1 values(1,'alex','male',20);
# 往id字段插入数据
insert into t1(id) values(2),(3);
2)查看表结构
txt
# 查看表结构,可简写为desc 表名
mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
# 查看表详细结构,可加\G
mysql> show create table t1\G;
*************************<strong> 1. row </strong>*************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
3)修改结构表 语法:
txt
1. 修改表名:
alter table 表名 rename 新表名;
2. 增加字段:
alter table 表名 add 字段名 数据类型[完整性约束条件...],
add 字段名 数据类型[完整性约束条件...];
alter table 表名 add 字段名 数据类型 [完整性约束条件…] FIRST;
alter table 表名 add 字段名 数据类型 [完整性约束条件…] AFTER 字段名;
3. 删除字段
alter table 表名 drop 字段名;
4. 修改字段
alter table 表名 modify 字段名 数据类型 [完整性约束条件...];
alter table 表名 change 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
alter table 表名 change 旧字段名 新字段名 新数据类型 [完整性约束条件…];
示例:
txt
1. 修改存储引擎
mysql> alter table service
-> engine=innodb;
2. 添加字段
mysql> alter table student10
-> add name varchar(20) not null,
-> add age int(3) not null default 22;
mysql> alter table student10
-> add stu_num varchar(10) not null after name; //添加name字段之后
mysql> alter table student10
-> add sex enum('male','female') default 'male' first; //添加到最前面
3. 删除字段
mysql> alter table student10
-> drop sex;
mysql> alter table service
-> drop mac;
4. 修改字段类型modify
mysql> alter table student10
-> modify age int(3);
mysql> alter table student10
-> modify id int(11) not null primary key auto_increment; //修改为主键
5. 增加约束(针对已有的主键增加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
6. 对已经存在的表增加复合主键
mysql> alter table service2
-> add primary key(host_ip,port);
7. 增加主键
mysql> alter table student1
-> modify name varchar(10) not null primary key;
8. 增加主键和自动增长
mysql> alter table student1
-> modify id int not null primary key auto_increment;
9. 删除主键
a. 删除自增约束
mysql> alter table student10 modify id int(11) not null;
b. 删除主键
mysql> alter table student10
-> drop primary key;
4)复制表
txt
复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;
只复制表结构
mysql> select * from service where 1=2; //条件为假,查不到任何记录
Empty set (0.00 sec)
mysql> create table new1_service select * from service where 1=2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create table t4 like employees;
5)删除表
txt
drop table 表名;
# 清空表,auto_increment = 0
truncate 表名;
3. 数据类型
1)数值类型 整型作用:存储年龄,等级,id,各种号码 浮点型作用:存储薪资,身高,体重,体质参数 2)日期类型 作用:存储用户注册时间,文章发布时间,员工入职时间,出生时间,过期时间 3)字符串类型 char和varchar(char) 4)枚举和集合类型
4. 完整的约束
1)约束条件not null与default not null 非空 default 默认值 zerofill:如果插入int(4),你插入1,显示0001 unsigned:不能为负数,当你插入负数就显示为0
txt
create table t1(
id int(11) unsigned zerofill
);
create table t1(
id int,
name char(6),
sex enum('male','female') not null default 'male'
);
2)约束条件unique unique约束唯一标识数据库中的每条记录 UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。 PRIMARY KEY 约束拥有自动定义的 UNIQUE 约束。 请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。 单列唯一:
txt
方式一:
create table department(
id int,
name char(10) unique
);
方式二:
create table department(
id int,
name char(10),
unique(name)
);
联合唯一:
txt
create table services(
id int,
name char(15),
port int,
unique(id),
unique(name,port)
);
3)约束条件primary key 约束:not null unique 不为空且唯一 存储引擎innodb:对于innodb存储引擎来说,一张表内必须有一个主键
txt
# 单列主键
create table t1(
id int primary key,
name char(16)
);
# 会自动呢找一个不唯一且为空的字段为主键
create table t1(
id int not null unique,
name char(16)
);
# 复合主键
create table t1(
id char(15),
port int,
primary key(id,port)
)
4)约束条件auto_increment
txt
create table t1(
id int primary key auto_crement,
name char(15)
);
# 了解
show variables like 'auto_inc%'; 查看与auto_inc模糊查询
# 步长
auto_increment_increment默认为1
# 起始偏移量
auto_increment_offset默认为1
# 设置步长
set session auto_increment_increment=5;
set global auto_increment_increment=5;
# 设置起始偏移量(起始偏移量<=步长)
set global auto_increment_offset=3;
5)约束条件foregin key 用来建立表之间的关系 可以发现下表中的部门和部门信息重复 id | name | sex | dep_name | comment ---|---|---|---|---| 1 | alex | male | 技术部| 技术 2 | sjingx | male|技术部| 技术 3| wuxx| female| 财务部| 钱 4 |liunx| male| 生成部| 生产 5 |python| male| 技术部| 技术 forrgin key的作用:方便管理,节省磁盘空间 示例: user_info表 id|name|sex|dep_id ---|---|---|---| 1|alex|male|1 2 | sjingx | male|1 3 | wuxx | female|2 4 | liunx | male|3 5 | python | male|1 dep表 id|name|comment ---|---|---| 1|技术部|技术 2|财务部|钱 3|生成部|生产
txt
# 先建被关联的表
create table dep(
id int,
name char(16),
comment char(50)
);
# 再建关联的表
create table user_info(
id int primary key,
name char(10),
sex enum('male','female'),
dep_id int,
foreign key(dep_id) references dep(id) on delete cascade on update cascade
);
on delete cascade删除同步:被关联一删除,关联的表也删除了
on update cascade更新同步:同上
# 先往被关联表插入记录
# 在往关联表插入记录
# 删除