Mysql基本操作

2022/9/2 2:22:45

本文主要是介绍Mysql基本操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

mysql
数据库管理软件
底层还是文件
操作不用IO流
使用sql语言

数据库 database
表 table
列 column
数据 data

cmd控制台里操作
-uroot -p
show databases;展示所有数据库;
create database 数据库的名字;
use 数据库名; 切换数据库
create table 名(id int, name varchar(长度),age int,phone cha(11));
select*from 名;查询表中的内容;

给表添加数据 insert into
insert INTO 名 values(1,'andy',18,'1399999');

sql语言 结构化查询语言

关系型数据库软件分类
1.mysql Apache oracle
2.oracle Oracle 公司
3.db2 IBM
4. sqlserver 微软


--DDL 数据定义语言
结构的操作
创建库
create database db_name;
删除库
drop database db_name;
使用数据库
use db_name;
创建表结构
create table tb_name( fieldName type(length),......);
修改表结构
alter table tb_name
add fieldName type(length); //添加列类型

alter table tb_name
modify fieldName type(length); //修改列的类型

alter table tb_name
drop fieldName; //删除表结构
删除表
drop table tb_name;

decimal(8,2) 小数类型 第一个参数是总长度 第二个是小数点后面保留的位数


DML 数据操作语言
1.添加数据
insert into tb_name values(val1,val2...);
不需要填的列对应位置可以填null,也可以:
选择列插入
insert into tb_name(col1,col2,col3...) values(val1,val2,val3...);

2.修改数据
update tb_name set col1=val1,col2=val2...where col = val;

3.删除数据
delete from tb_name where col1 = val1io。

4.查询
select 选择哪些列(字段) '*' 表示所有列
from 从哪个表中查询
// 查询emp所有的内容:
//select * from emp;
now()获取当前日期 year(now())获得当前日期的年份
起别名 as ' ' 可以省略
where 筛选表中的行
根据where后面的条件对表中所有行进行逐一判断,若符合要求,则将当前行(记录)存入结果集
like 像 模糊查询
not like 不包含

/ 转义符
escape 自定义转义符 escape '@' @就成了转义符 /的功能就消失了 ;
% 通配符 代替>=0个字符
_ 通配符 代替1个字符
//查询名字以li开头的员工信息
select * from emp where name like 'li%';
//查询姓名中包含s的员工信息
select * from emp where name like '%s%';
> < >= <= = (!= <>)
-- and &&
-- between and
select * from emp where salary between 4000 and 5000;
--or 或者 ||
--in(val1,val2....) not in(....);
select * from emp where salary in(4000,4800,5000);

-- 聚合函数 max() min() avg() sum() count()
//查询最高工资
//select max(salary) from emp;
//查询平均工资
//select avg(salary) from emp;
count 统计符合条件的数
select count(*) from emp where salary between 4000 and 5000;
-is null; 该列为null
-is not null;
冗余数据
distinct 去冗余

order by 排序 默认排序为升序 asc可以不写;
//所有员工按工资升序
//select * from emp order by sal;
//若工资相同 按奖金降序 (desc;降序)
//select * from emp order by sal asc,comm desc;

limit 取出结果的某几行
第一参数是从哪里取 , 偏移量
第二个参数是取几个
分页展示
每页展示几行
select * from emp limit 0,10;

主键 每个表只能有一个主键 不重复 不为空!!
primary key;
也可以有联合主键
primary key(col1,col2...)
自增 auto_increment; 设置当前列为自增
not null; 当前列不能为空

group by 分组查询 将分组字段的相同值分成一组,再对不同组做处理
select 后跟分组字段和聚合函数
select class,count(*) from student group by class;
having 分组的筛选
select cno,avg(degree)
from score
group by cno
having avg(degree)<80;



这篇关于Mysql基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程