参考:http://www.cnblogs.com/wupeiqi/p/5713315.html
单机程序(自己DB)
单机程序(公用DB)
MySQL:是用于管理文件的一个软件
- 服务端软件
- socket服务端
- 本地文件操作
- 解析指令【SQL语句】
- 客户端软件(各种各样)
- socket客户端
- 发送指令
- 解析指令【SQL语句】
PS:
- DBMS数据库管理系统
- SQL语句
技能:
- 安装 服务端和客户端
- 连接
- 学习SQL语句规则;指示服务端做任意操作
其他类似软件:
关系型数据库:sqllite,db2,oracle,access,sql server MySQL
非关系型数据库:MongoDB,redis
MySQL在linux下安装
在安装过程中还需要设置密码。
mysql -u root -p(连接)
show databases;(数据库)
show tables;(表)
create database 数据库名;
创建用户及授权
创建的用户登录
操作文件夹、操作文件、操作文件内容
学习SQL语句规则
操作文件夹
create database db2;
create database db2 default charset utf8; (兼容中文)
show databases;
drop database db2;
操作文件
show tables;
create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10)) engine=innodb default charset=utf8; #engine = innodb,传输时中断,数据不会出错。出错回滚回原状态。
create table t3(id int auto_increment primary key,name char(10))engine=innodb default charset=utf8;
create table t1(
列名 类型 null, #可以为空
列名 类型 not null, #不允许为空
列名 类型 not null auto_increment primary key,
id int,
name char(10)
)engine=innodb default charset=utf8;
操作文件中内容
在表中插入数据:insert into t1(id,name) values(1,'alex');
可以为空null
auto_increment,id自增
一个表中只能有一个自增列,自增的,必须是主键,auto_increment primary key,primary key对该列进行约束,不能重复,不能为空,可以快速查找。
默认值
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2, #默认值
num int not null
)
清空表
delete from t2; #清除表中的内容,再添加时会从上一次自增id处开始。
truncate table t1; #清除表中的内容,再添加时会从1处开始,而且速度块。
delete from t1 where id<6;
改:update t1 set age = 18 where age =17; #age 17全改为18
删除表
drop table t1;
表内容操作
1、增
1
2
3
|
insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表 |
2、删
1
2
|
delete from 表 delete from 表 where id=1 and name = 'alex' |
3、改
1
|
update 表 set name = 'alex' where id>1 |
4、查
1
2
3
|
select * from 表 select * from 表 where id > 1 select nid, name ,gender as gg from 表 where id > 1 |
修改表
|
添加列:alter table 表名 add 列名 类型 删除列:alter table 表名 drop column 列名 修改列: alter table 表名 modify column 列名 类型; -- 类型 alter table 表名 change 原列名 新列名 类型; -- 列名,类型 添加主键: alter table 表名 add primary key(列名); 删除主键: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); 删除外键:alter table 表名 drop foreign key 外键名称 修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; 删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT; 改变添加位置: alter table 表名 auto_increment = 3;
|
数据类型
数字:
tinyint
int
bigint
FLOAT
0.00000100000123000123001230123
DOUBLE
0.00000000000000000000100000123000123001230123
0.00000100000123000000000000000
decimal(小数时推荐) num decimal(10,5) (总位数,精确数)
0.1
字符串:
最多255个字符
char(10) 速度快()
root 未沾满则填充满
varchar(10) 节省空间
root 占四个位置,节省空间,定长
PS: 创建数据表定长列往前放
text (长度比char大)
上传文件:
文件存硬盘(过大文件不适合放数据库)
db存路径
时间类型
DATETIME
enum枚举 插入时只能选择枚举中一个
set集合 只能插入集合set('a', 'b', 'c', 'd')中的任意组合'a, b'
外键:
department与id对应。
外键:
节省空间
create table userinfo(
uid int auto_increment primary key, 自增
name varchar(32),
department_id int,
xx_id int,
constraint fk_user_depar foreign key (department_id) references department(id) #约束department 与 id对应
)engine=innodb default charset=utf8;
create table department(
id bigint auto_increment primary key,
title char(15)
)engine=innodb default charset=utf8;
自增列起始值与步长
alter table 表名 auto_increment = 20;
基于会话级别:
show session variables like 'auto_inc%'; 查看全局变量
set session auto_increment_increment=2; 设置会话步长,该设置的步长只在当前窗口下有效
基于全局级别:
show global variables like 'auto_inc%'; 查看全局变量
set global auto_increment_increment=2; 设置会话步长
唯一索引 能确定某个数据列将只包含彼此各不相同的值(如身份证号)
create table t1(
id int ....,
num int,
xx int,
unique 唯一索引名称 (列名num,列名xx), #联合唯一索引
constraint ....
)
#
id num xx
1 1 1 联合索引唯一 这一行
2 1 2 与这一行不同
PS:
唯一:
约束不能重复(可以为空)
PS: 主键不能重复(不能为空)
加速查找
打开和停止服务
查看新建的数据库nzp
练习:http://www.cnblogs.com/wupeiqi/articles/5729934.html
答案:https://www.cnblogs.com/wupeiqi/articles/5748496.html
ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
因为你要添加外键时,你的外键类型是int
而引用的主键类型也是 int,如果你的主键设置auto_increment(那么你的字段类型实际上是unsigned_int)
主键,外键类型不一致,所以无法添加外键。
所以此时将我们的外键字段类型改为无符号整数。那么就可以成功添加外键了。