MySQL基本操作

发布时间 2023-12-05 14:12:43作者: 33呀!

//mysql 数据库管理工具 简称叫 数据库(存放数据,作为动态网站开不可缺少的一环)

mysql 是一种关系型数据库

基本语法:

1.查询当前MySQL 下有的所有数据库

show databases;

2.创建数据库

create database 数据库名 数据库选项(字符集,校对集)(大部分情况我们都不进行数据选项的设置)

create database 数据库名;
create database w001;
create database ;

3.删除数据库

drop database 数据库名; (慎用)

drop database wei001;

4.选择数据库

use 数据库名;

出现以下提示

Database changed 则表示数据库已经选择完毕

/*************************************************************/

表操作:

create table 表名(
    字段名称1 字段属性(类型) 字段选项,
    字段名称2 字段类型 字段选项,
    ........................
)表选项;

表选项:表字符集和数据引擎
字符集:charset=utf8;
数据引擎:默认innodb 语法:engine=引擎

字段类型:
整数型 int(大部分情况我们都使用这种-19亿-19亿) tinyint(微整型-127-128) smallint(小整型-3万-3万) bigint(大整型 能存储亿万级别的数据)
小数型 float(浮点型) decimal(定点型)3000.00 decimal(10.2)一共10位数,小数点后占2位 10000000.00
字符串 char(定长型) varchar(变长型) 在mysql中 一个中文文字占2,3?字节,英文字母一个占1字节
char(12) XX XXX 不管存储进去的文字是多少字节,如不够12字节,就自动补充至12字节
varchar(12) XX(6) XXX(9) 该种数据类型会更加数据文字的情况自行变更字节量,更加灵活
日期
date 日期 '2023-5-11' 一定要用引号包裹
datetime 日期加时间 '2023-5-11 17:41:33'

字段选项 (选择性添加)
primary key 主键:具备唯一特点 不能为空 一般也设置在唯一的字段上 (一般来说每一张表最好具备一个主键)
auto_increment 自动增长 必须设置在 int 类型的字段上才能实现该功能 一般也设置在主键的字段上
default 默认值 为一个字段设置默认值
not null 不能为空 即该字段必须填写数据

/表的创建*/

create table student(
    s_id int primary key auto_increment,
    s_name varchar(33) not null,
    s_sex char(3) default '男' not null,
    s_age int default 18 not null,
    php int default 0 not null,
    Vue int default 0 not null,
    Node int default 0 not null
);

/表的修改*/
//查询该数据库下的所有表

show tables;

//查询数据库某表结构

desc 表名;

//修改表
//修改表名

alter table 旧的表名 rename to 新的表名;
alter table student rename to student1;

//修改(在表中添加一个字段 通常是表中没有数据的情况下 才可以添加)
alter table 表名 add 新字段 字段类型 字段选项;
alter table student1 add 编程 int default 0 not null;

//修改表的字段
alter table 表名 change 旧字段名 新字段名 新字段类型 新字段选项;
alter table student1 change 编程 大编程 int default 0 not null;

//删除字段
alter table 表名 drop 字段名;
alter table student1 drop 大编程;

/表的删除*/

drop table 数据库名; (慎用)

/******************************/

数据操作

增 (向表中插入数据 (添加数据))

insert into 表名(字段1,字段2,···字段n) values (实际数据1,实际数据2,···实际数据n);

insert into student1(s_id,s_name,s_sex,s_age,php,Vue,Node,大编程) values (2,'张三','男',20,70,80,60,90);

insert into student1 values (2,'张三','男',20,70,80,60,90);

insert into student1 values (null,'李四','男',80,34,56,67,78);

insert into student1 values (null,'小李',default,60,345,67,62,50);

//一次性插入多条数据

insert into student1 values (null,'小蕾','女',18,35,64,52,90),
                            (null,'小花','女',20,40,75,62,64),
                            (null,'小明',default,18,70,50,30,93),
                            (null,'大明',default,23,33,78,81,50),
                            (null,'老六',default,25,85,73,69,96),
                            (null,'小李子',default,20,72,82,66,99),
                            (null,'大李子',default,18,77,81,62,91);

查 (重点)
查询整张表的数据

1.select * from 表名;

*:通配符 代表所有

select * from student1;

2.选择性查询

select 需要查询的字段1 as 别名1,需要查询的字段2 as 别名2,···字段n as 别名n from 表名;
select s_id,s_name,php from student1;

2-1.取别名

select s_id as 学号,s_name as 姓名,php as 成绩 from student1;

3.条件查找

select * from 表名 where 条件;
select 需要查询的字段1,需要查询的字段2,···字段n from 表名;

select * from student1 where s_sex='女';

select s_name,s_sex,php from student1 where s_sex='女';  

4.多条件查询 and/&&(与 需要同时满足多个条件) or/|| (或 多个条件只需要满足一个即可)

select s_name s_sex,Vue from student1 where s_age<18 and s_sex='男';

select * from student1 where php<60 or Node<60; 

条件查询的模糊查找 关键字 like 占位符 %:不管后面有多少字 都显示出来 _一个下划线表示一个字 可以通过这种方式来更进一步来模糊查找不同字数的数据

select * from student1 where s_name like '小__';

mysql中的方法
group by字句: 分组 一般要具备分组的意义 比如我们用性别进行分组 可以分为男生组和女生组
注意: 分组只能是分组后的数量而不能是分组后的组里信息
select 字段或聚合数据(函数) from 表名 group by 分组字段;
常用聚会函数有: 取平均值avg(),取个数count(),取总和sum(),取最大值max(),取最小值min()
聚合函数计算的是列 因为聚合函数括号中放的是字段

select s_sex, avg(php) from student1 group by s_sex;

select * from student1 where (php+Vue+Node+大编程)/4>=80;


order by: 排序

分: 正序(asc) 和 降序(desc)

select * from 表名 where group by having order by 条件 asc|desc limit

select * from 表名 order by 要排序的字段1,排序方式 (升序asc/降序desc)
select * from 表名 order by 要排序的字段1,排序方式 (升序asc/降序desc),要排序的字段2, 排序方式 (升序asc/降序desc)
select node from student1 order by node desc;
select node from student1 order by node asc;

select (php+Vue+node+大编程) as 总成绩 from student1 order by (php+Vue+node+大编程) desc;

limit: 分页查询
语句:
limit 0,3;
limit 3,3;
limit 6,3;
limit 9,3;

select * from student1 limit 2,5;     

说明:
第一个数是开始数据的下标
第二个数是每次显示的数量

delete from 表名;
删除整张表的所有数据

delete from 表名 where 条件;
delete from student1 where Node>60;