MySQL 的基础语句学习

create database  stu ;
-- 创建  数据库 stu

create database  stu  character set utf8;
-- 创建数据库 并指定 字符集 utf8  (gbk,也是中文字符集)
show create database stu;
-- 查看 数据库 stu 的创建语句  查看数据库stu 使用的字符集

drop database stu;
-- 删除数据库 stu 

show databases ;
-- 查看所有的数据库名称

alter database stu character set gbk;
-- 修改数据库使用的字符集 

select database() ;
-- 查看目前使用的数据库名称

use stu;
-- 切换数据库

数据库的创建 与删除 字符集的修改 

数据库的基本数据类型

int 整数类型 默认 10个字节

char  字符串 类型    不可变

varchar  字符串类型  可变

double   小数类型

date  日期

datetime   日期

timestamp  时间戳类型  不赋值自动根据时间赋值

varchar 与char 的区别

varchar  长度可变  不超过设定的最大长度  书写几个字节就记录几个字节 优点节省空间

char长度固定 设定多少字节 就记录多少  书写几个字节不足设定 长度, 空格补齐  优点,效率高

操作表的基础命令

create table student (
id   int,
name varchar(32),
tel  char(11),
salary double,
entry   date
);
--创建 student 表格 其中有 ID 姓名 电话 薪资 入职时间

desc student ;
-- desc 表名 查看表结构 

alter table student add  address varchar(32);
-- 为student表添加新的列 address 数据类型 varchar

alter table student modify  tel  int(11);
-- 修改 电话  的数据类型   modify   
alter table student drop   salary;
-- 删除  薪资   
show create table student ;
-- 查看 student 表的字符集  创建语句   

alter  table student charset gbk;
-- 修改 student表的字符集
rename  table student to  person;
--修改  表名称   
show tables; 
-- 查看当前数据库所有的表名称
drop  table if exists person ;
-- 如果 表person 已经存在 那就删除

约束 

primary key  主键      foreign key 外键  not null 不为空  unique  唯一约束 

auto_increment  自增长 

唯一约束 与主键的区别 

唯一约束可以为空  在一个表中可以拥有 多个 

主键不能为空  且只有一个

comment " "注释  

DML 数据库操作语言 

insert into 表名(  ) values(  );

create table student (
class int  not null comment'班级',
name  varchar(200) not null comment'姓名',
chinese double,
math    double,
english double ,
physics double,
biology double,
chemistry double
);
-- 创建 表 
insert into student (class,name,chinese,math,english,physics,biology,chemistry) values( 3,'李三',89,95,74,83,92,67);
-- 列名 必须与列值 一一对应 
insert into student (class,name) values( 3,'李四');
-- 可以省略  
insert into student values(3,'李青',89,95,74,83,92,67),(2,'花平',98,67,85,85,67,58),(1,'宋江',57,69,98,45,34,87),(2,'林冲',76,87,90,99,89,87);
-- 批量添加数据
insert into student values(3,'李威',89,67,68,56,90,67),(1,'王武',90,87,98,77,87,86),(2,'路飞',87,68,67,54,86,86),(1,'马云',88,90,88,90,79,93);

insert into student values(3,'聂风',78,80,79,79,80,89),(2,'无名',80,79,89,79,79,80);

删除 数据 

delete from student where class=3;
-- 删除 班级为3的学生信息

delete  from student;
-- 删除  表内所有数据 

truncat table student;
-- 将表删除 再创建一个没有数据的一模一样的表

delete 删除数据是逐条删除 ,效率低 且如果有自增长的列 ,不会清空自增长记录.

update student set name='花舞' where id=2;
-- 不加条件会改变所有的name

DQL 数据查询

select 字段列表

from 表名列表 

where 条件列表 

group by  分组字段 

having  分组后的条件

order by  排序

limit  分页限定 

distinct  去重复 

-- 去重复  
select  distinct name from student ;

-- 去除 名字重复的数据
 
select distinct name,class from student ;
-- 去除 名字重复 且 班级重复的学生信息
  
select * from student ;

-- 通用查询  查询 student表中所有数据

select name,math+10 from student;
-- 查询 student表 name  数学成绩 且为数学成绩+10;

select math+english from student ;
-- 如果math 或者english 为null 那么相加结果为null 
--  因为null 参与运算时 运算结果都为null,使用ifnull解决这个问题

-- ifnull(参数1,参数2)
-- 如果参数1是 null 那么就输出参数2 如果参数1不为null 那就输出参数1

条件查询 

select name,math from student where class=3;
-- 查询 班级为3的同学的名字 数学成绩

select name from student where class=3 and math>80;
-- 查询 班级为3 且math成绩 大于80的同学名字

select name from student  where class=1 or class=2;
-- 查询班级为2或3 的同学名字

select  name from student where class in(1,2,3);
-- 查询班级1,2,3 同学的名字
select name from student where math between 80 and 90;
-- 查询 math 成绩在80-90 之间的同学名字

模糊查询
select * from student where name="王%";
-- 查询姓氏为王的同学的全部信息

-- %花%  包含花   %花 以花结尾 -单个占位符   % 多个任意字符
 

排序查询 

order by  列名 ASC 升序

order by 列名  默认值 升序 

order by 列名 DESC 降序 

order by  列名,列名 asc;

两个降序 条件   以第一个为基础 第二个为辅助  第一个重复的情况下 比较第二 条件 

常用的聚合函数

select name ,max(math) from student where math<80;
-- max 最大值 
select name ,class, min(chinese+math+english+physics+biology+chemistry) from student ;
-- 最小值 
select class, avg(chinese) from student where class=2;
-- 平均值 
select avg(math) from student group by class;

select sum (math) from student ;
-- 求和 

select count(*) from student;
-- 计算有多少条数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值