2021-08-03

MYSQL的基础知识(持续更新)
数据库分类:
关系型数据库 :以一种关系模型(二维表格模型)组织数据的数据库
分类:oracle、mysql、sql server、mariadb、sqlite(轻便、直接引入头文件).
非关系型数据库:不基于sql实现的数据库
分类:redis(后期学习)、memcached、mongodb

mysql数据库的安装:(Linux)
安装:yum -install -y mariadb
查看配置:
mysql -uroot;/mysql -uroot -p;(带密码)
show variables like ‘%char%’;

客户端操作:mysql -uroot -p(带密码)
注意事项:
1.sql语句中,都已“;"结尾;
2.sql语句中,不分大小写;
3.sql语句中,数据库名称,表名称,字段名称不能直接使用sql关键字;(加name防止)``

数据库的基本操作:库的操作、数据类型、表的操作
库的操作:
创建数据库:create database dbname;/ create database if not exists dbname;
查看数据库:show databases;
选择所使用的数据库:use dbname;
查看所使用的: select database();
删除数据库:drop database dbname;
常用数据类型:
整形:bit(16) , tinyint

浮点型:float(m,d),double(数字个数,小数个数),decimal(m,d) , numeric(m,d)
字符串型:char(32)固定字符个数,varchar(32)最多存储32个字符,变化,test ,mediumtext ,blob

日期型:datatime
 decimal(m,d)-浮点数; varchar(可变字符长度);char(固定字符长度)

1
2
在这里插入图片描述

表的操作:
create table if not exists tbname();
1
查看表:desc tbname;
删除指定表:drop table tbname;
修改表结构:alter关键字;–
表中字段约束
primary key 主键约束 约定指定字段的值-非空且唯一
unique key 唯一约束 约束指定字段必须唯一
not null 非空约束-约束指定字段的值不能为NULL
auto_increment 自增属性(只能用于整形的主键字段)
create tbname if not exists tbname(id int primary key auto_increment,name varchar(32) not null unique, sex bit,score decimal(4,2),birth datetime);
表中数据的操作:增删查改
新增:
intsert tbname(id,name,sex) values(null,“张三”,0);
insert tbname values(null,“李四”,1,22.33,“2021-6-10 12:00:12”);
删除:
delete from tbname where id=2;
修改:
update tbname set score=66.54,birth=“2021-10-2 12:20:22” where id=1;
查询:
查询表中所有数据:select * from tbname ;
指定列查询:select id, name, score from tbname;
查询两数相加结果:select id+score from tbname;
给字段取别名:select id+score as newname from tbname;(as可省略)
去掉重复的:select distinct height from tbname;去除tbname表中height重复的数据,相同height只显示一次;
按序查询:select id, name ,score from tbname order by id desc;降序desc,升序asc,默认升序;
select * from tbname order by height desc,age asc;按照height降序排列,如果一样按照age升序排列;
分页查找:select * from tbname order by id desc limit 3 offset 3;
select * from order by id desc limit count offset page* count;
条件查询:where
关系运算符:
比较:>,<,>=,<=,=,!=,<=>,<>
select * from tbname where score=66.55;

运算符 说明

, >=, <, <= 大于,大于等于,小于,小于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <> 不等于
空值:is null,is not null
select * from tbname where score is null;
范围:between …and…
select * from tbname where score between 65 and 80;
子集匹配:in(集合)
select * from tbname where score in(66.55,88,55.3);
模糊匹配:like
select * from tbname where name like ‘%飞%’;
逻辑运算符:and、or、not
与:双目,and-链接两个比较条件,两者同为真则结果为真
select * from tbname where score>=60 and score<=80;
或:双目,一个为真就为真
select * from tbname where score>=61 or id<=10;
非:单目运算符,条件为真,则为假
select * from tbname where not score>=60;
注:

WHERE条件可以使用表达式,但不能使用别名。
AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值