- 1、mysql中的utf8 中间不写“ - ” ,且不区分大小写
- 2、sql语句 以“ ;”结尾,ctrl + c 强制终止当前 sql语句
- 3、mysql 中 数字与 字符串可以进行加减运算(原理:将字符串转化为数字后与数字进行运算)
一、mysql 的基本知识:
1、数据库系统(DBS)、数据库(DB)、数据库管理系统(DBMS)
(1)DBS :它是 数据库与数据管理系统的统称
(2) 数据库(DB):它是用于存储数据的
(3)数据库管理系统(DBMS):类似于图书馆的管理人员,记录元数据的
2、mysql 的特点:
它是一个内部为链表结构的关系型数据库,具有轻量高性能的特点。
3、mysql 与 sql 的关系:
sql 是一门 多种数据库的交互语言,当然也包括 mysql
mysql 只是多种数据库中的一种。
4、mysql 的组成:
① 数据库(DB):保存在文件夹 的data 目录下,数据库的第一层级的 元素是 数据库的对象(databases),而
每一个数据库的对象内部 则存储 表格形式的 数据(tables)。
② 其他:其他目录相当于 管理系统DBMS
二、mysql 的常见命令:
常见的命令关键字:
1、mysql的系统命令:
(1) 登录mysql 的命令:
命令 -------- bin目录下: mysql -h 服务器地址 -u 用户名 -p
例: mysql -u root -p ---------------- 默认登录本地
(2)查看mysql 所有的存储 引擎 engines :
命令: show engines;
(3)查看当前默认的存储引擎 engine:
命令:show variables like "%storage_engine%";
更改默认引擎:
https://blog.csdn.net/qq_16555103/article/details/86081226
常见的存储引擎:
2、 数据库database 命令:
(1)查看database:show
命令: show databases;
(2)创建数据库:create
命令: create database `你的数据库名`; 当数据库名是系统关键字时,需要用 ` ` 波浪号引起来, 代表转义
(3)使用或打开某一个数据库:use
命令:use 数据库名;
(4)删除数据库:drop
命令:drop database 你的数据库名;
3、table定义信息的增、删、改、查:
(1)表格创建:create
① 命令: 数据库内部
create table 表名(
字段1 类型, ------------- 字段(feild)是 表格的列名
字段2 类型,
.....
);
例:
create table username(
name char(11),
age int,
phone char(11) --------------- 最后一个 元素不能加 " ; "
);
- windows 数据库中:
- int ----------- 数字
- char 、varchar ------------- 字符串
② 数据库外:
create table 库名.表格名(
name char(11),
age int,
phone char(11)
);
(2)查看表格:show 、desc
① 查看该数据库中所有的表格:
命令: show tables;
② 查看 表格的描述信息(即定义时表格):
desc table名;
③ 查看 表格创建的sql语句:
show create table table名;
(3)删除表格: ------- 包括表格结构
① 直接删除 drop --------------- 注意 与 truncate 、delete from 的区别
命令: drop table table名;
(4)表格的修改:
- modify 和 change 区别, 如果 要改 字段名,用change ,而使用modify可以修改数据类型和约束。
1) alter table 修改
① 修改表名: rename
alter table 旧表名 rename to 新表名;
alter table old_name rename to new_name;
② 修改字段名 :change
2.1、 只修改字段名 :
alter table 表名 change 旧字段名 新字段名 旧数据类型;
alter table staff change id staff_id int;
2.2、 同时修改字段名和数据类型 :
alter table 表名 change 旧字段名 新字段名 新数据类型;
③ 添加字段 :add
注意:添加字段 没有 before
3.1 添加 字段到 第一列:
alter table 表名 add 新字段名 数据类型 first;
alter table staff add department_id int first;
3.2 添加 字段到最后一列:
alter table 表名 add 新字段名 数据类型; ---------------------- 默认为最后一列
3.3 在某列之后添加字段:
alter table 表名 add 字段1 数据类型 after 字段2; ----------- 将字段1 添加到 字段2 后面(相邻)
alter table staff add address char(50) after department;
④ 删除字段:drop
alter table 表名 drop 字段名;
⑤ 修改字段的 数据类型:modify
第一种: alter table 表名 modify 字段名 新数据类型;
第二种 :alter table 表名 change 字段名 字段名 新数据类型; ------------------- 利用change使得前后字
段名相同
⑥ 修改字段 的位置:first 、after
特点: 没有 before
alter table 表名 modify 字段名 数据类型 first; //修改顺序到最开头
alter table 表名 modify 字段名 数据类型 after 字段名2; //修改顺序到 字段名2 后
alter table staff modify department char(10) after class;
三 、mysql 字段的数据类型:
1、整数类型:int
2、浮点类型:float 、double
3、定点数类型:dec(m,d)
- m --------- 指的是 该数字 整数和小数 位数之和 d ---------- 小数的位数
特点:它与 前面的数字类型不同,保存在 机器上是以字符串类型,因此具有 较高的 精度。
4、时间的日期类型:
(1)获取当前时间的语句:
select date_format(now(),"%Y-%m¥%d");
select date_format(now(),"%Y-%m-%d %H:%i:%s");
select date_format("2015-12-22","%Y"); ------------ 取年份
select current_timestamp();
(2)计算时间差:
- mysql 中 数字与 字符串可以进行加减运算(原理:将字符串转化为数字后与数字进行运算)
update `order` set price=5 where (date_format(now(),"%Y")-2015)>=3;
update `order` set price=5 where (date_format("2018-12-25","%Y")-2015)>=3;
(3)从日期数据类型中截取时间:year()、month()
1、date_format("时间","格式模板")
update `order` set price=5 where (date_format(now(),"%Y")-2015)>=3;
update `order` set price=5 where (date_format("2018-12-25","%Y")-2015)>=3;
2.获取当前系统年份:year()
update `order` set price=5 where year("2018-12-25")-2015 >= 3;
获取当前系统年份:select year(CURRENT_DATE) AS 年 ;
获取当前系统月份:select month(CURRENT_DATE) AS 月;
获取当前系统日:select day(CURRENT_DATE) AS 日;
链接: https://blog.csdn.net/michae_xiaowu/article/details/78558884
5、字符串类型:
(1) char(M) 与 varchar(M)
注意: M -------------- 是定义的字节数,必须填写,否则默认为最大值,浪费空间
(2)text 类型的字符串:
(3)binary (二进制)与 BLOB: 不常用
6、null 空的:
null 是缺省值,相当于一种数据类型,他并不是空的字符串
update student set email=null where stu_id=1001; -------------- 删除某一行的某个值
四、对table内的数据进行操作:
create 创建表格 只是创建表格的结构,里面没有数据,因此需要给表格插入数据(insert into)
create table stutent(
id tinyint not null primary key auto_increment, --- 多个约束条件用空格隔开
stu_id int not null,
stu_nmae varchar(10) not null, ---- varchar()和char() 括号里面需要加内容{字节数},否则默认最大。存储时 字节数 + 1 ,最后一位 记录 存储数据的字节数。
grade int not null,
sex varchar(1) default "男",
age int,
phone varchar(11),
email varchar(30),
number_id char(40)
);
1、查看表格中的内容:select
用法:select 字段名 from table名;
select * from table_test;
select name,age,sex from table_test;
(1)select 语句筛选 :where
select * from student where (age >= 20 and sex='女') or (grade >=4003 and sex='男');
2、插入数据:insert into
用法:insert into table名 [(字段1,字段2,字段3)] value (值11,值12,值13),(值21,值22,值23);
insert into goodorder (id,user_id,goods,num) value(1,1001,"Iphone X",3); ----- 括号 内部用 , 隔开
insert into goodorder value (1,1001,"Iphone X",5000,3,15000); --------- 前面不写 字段时 默认选中所有字段
insert into goodorder value (1,1001,"Iphone X",5000,3,15000),(2,1002,"HUA WEI P20",5000,2,10000)
,(3,1003,"HUA WEI P10",5000,4,20000),(4,1004,"HUA WEI P30",5000,5,25000);
// 插入多行数据时 用()表示每一行数据,并且用 “ , ” 隔开
3、修改表内部数据:update
用法:update table名 set field1=value1,field2=value2,field3=value3 [where 条件];
注意: where 条件 一般都会 添加,否则 将修改整列数据。
update goodorder set num=1,total=5000;
update goodorder set num=1,total=5000 where id = 4;
(1)字段运算更新:字段可以看做变量
例如:
update goodorder set num=num+1,total=5000+num where id = 4; ----- 操作字段相加并更新。
4、删除表内部数据:
(1)delete from:
用法:delete from table名 [where 条件];
注意: where 条件 一般都会 添加,否则 将删除表内全部数据。
delete from student;
delete from student where stu_name='小龙女' and address ='终南山';
(2)truncate :
用法:truncate table table名;
truncate table student;
- 它与 delete from 的区别:当表中存在 自增主键时 ,truncate可以所有的数据和记录,delete from 只能删除数据
(3)删除某一行元素的某个值:用 null 修改更新
update student set email=null where stu_id=1001; -------------- 删除某一行的某个值
(4)drop : drop table table名;
drop 会删除 整个表格,包括表格的结构,而delete from和truncate都不会删除表结构。
五、mysql 的备份:
https://blog.csdn.net/qq_16555103/article/details/86094908
六、where 判断语句
特点:当 后面的语句 bool值为true时执行,类似 if 语句
select * from student where grade in (4001,4003,4004); --- in 类似python中 in的 用法,可以代替多个 or 逻辑
select * from student where grade not in (4001,4003,4004);
等价于:
select * from student where grade=4001 or grade=4003 or grade=4004);
where条件语句
1.年龄在x和y之间的,包含xy
age between x and y // 满足 age>=x and age<=y 前闭后毕
2.大于等于大三的男生 或者 大于等于20岁的女生
select * from student where (age >= 20 and sex='女') or (grade >=4003 and sex='男');
3.想要 4001,4003,4004班的人
select * from student where grade in (4001,4003,4004);
4.不想要 4001班的人:
select * from student where not(grade=4001); // not() 需要加括号使用
1、select、where、group by、having 执行顺序:
七、创建表的 约束条件:
常见的 字段约束:
- not null、 default、auto_increment 这三种 字段约束不会添加索引(键)名
- primary key 、foreign key 、unique 这三种 字段约束 会默认 添加 索引(键)名
实体样例:
create table foot(
id int(10) not null primary key auto_increment comment "编号", ------- 多个约束条件中间用 空格 隔开
name varchar(20) not null comment "食品名称",
company varchar(30) not null comment "生产厂商",
price float not null comment "价格",
produce_time year not null comment "生产年份",
validity_time int(4) not null comment "保质期",
address varchar(50) not null comment "厂址"
);
1、not null、auto_increment 、default 约束的增、删、查:
(1)查看 约束:可以获得约束的键名信息
命令: show create table table名; ------------------ 查看创建表格的 sql 语句
(2)增添 约束:
① 在创建表格时进行 添加:
create table Test(
id int not null default "1001" 字段的数据类型是必须的,且必须放在前面
);
② 通过 alter 修改字段添加:
方法一:alter table table名 modify id int not null default "1001" auto_increment;
方法二: alter table table名 change id id int not null default "1001" auto_increment;
- 特点: alter 修改字段添加 时 ,需要将 所有的 约束都要写出,没有写的 字段约束 还原默认。
(3) 删除 约束:
通过 alter 修改字段删除 :
alter table table名 modify id int; -------------- 约束 条件未填写还原 默认
2、unique 、primary key、foreign key 约束 增、删、查:
- 特点 :这三种情况默认会加上索引,所以可以使用删除索引的形式删除
(1) 查看 约束:show
命令: show create table table名; ------------------ 查看创建表格的 sql 语句
(2) 添加 约束 :add constraint
第一种:创建表的时候添加
第二种 :修改表的时候添加 (注意:由于这三种情况默认会加上索引,所以不能用alter 修改字段删除 )
第三种 :通过 add constraint 添加:
- ---- key_name 字段约束索引(键)名 column_name 字段名 table_name 表名 foreign_name 外键名 -------
1) unique key:①:alter table table名 add unique key [key_name] (column_name);
②:alter table table名 add constraint [key_name] unique key(column_name);
③:create unique index 索引名 on table名(字段名([(长度)] [asc | desc]));
alter table pc add constraint key_unique unique key(memery);
alter table pc add unique key key2_unique(memery);
create unique index memery_indexName on pc(memery);
结果:
UNIQUE KEY `key_unique` (`memery`), 如果 字段约束索引名不自定义,默认与 字段名相同
UNIQUE KEY `key2_unique` (`memery`)
2) primary key:①:alter table table名 add primary key [key_name](column_name);
②:alter table table名 add constraint [key_name] primary key(column_name);
3) foreign key:
- 特点: 外键 父表 必须外键到 父表的主键字段。
大体格式: foreign key [key_name](column_name) references table_name(column_name) --- 外键 ... 连接到 ...
用法: alter table table名 add constraint [foreign_name] foreign key [key_name](column_name) references table_name(column_name) [选项];
// [key_name] 一般不需要
选项:
on delete cascade 主表删除,外键对应的行删除
on update cascade 主表更新,外键对应的字段也更新
on delete set null 主表删除,外键对应的字段设置为空
alter table little_brother add constraint waijian_name foreign key
(id_big_brother) references big_brother(id);
waijian_name 自定义外键名 id_big_brother 外键字段 references 链接 big_brother 链接的表名 id 链接表名的某个字段
(3)删除约束:
① primary key 删除:
alter table table名 drop primary key;
② unique key 删除:
1) alter table table名 drop index unique约束的键名;
2) drop index 索引名 on table名;
alter table pc drop index key2_unique; key2_unique 约束键(索引)名
drop index key2_unique on pc;
③ foreign key 删除:
alter table table名 drop foreign key 外键名;
alter table pc drop foreign key foreign_name;
下一节: https://blog.csdn.net/qq_16555103/article/details/86181736