开始MySQL之路—— DDL语法、DML语法、DQL语法基本操作详解

DDL语法

DDL(Data Definition Language) 数据定义语言,该语言部分包括以下内容。

  • 对数据库的常用操作

  • 对表结构的常用操作

  • 修改表结构

对数据库的常用操作

1: 查看当前所有的数据库

show databases;

2:创建数据库

create database if not exists 数据库名称;

create database 数据库名称;

3:选择使用哪一个数据库

use 数据库名称;

4:删除数据库

drop database 数据库名称;

drop database if exists 数据库名称;

5:修改数据库编码

alter database school character set utf8;

对表结构的常用操作-创建表

1:创建表格式

create table [if not exists] 表名(

字段1 类型[(宽度)] [约束条件] [comment '字段说明'],

字段2 类型[(宽度)] [约束条件] [comment '字段说明'],

字段3 类型[(宽度)] [约束条件] [comment '字段说明'],

)[表的一些设置];

创建表是构建一张空表,指定这个表的名字,这个表有几列,每一列叫什么名字,以及每一列存储的数据类型。

2:数据类型

数据类型是指在创建表的时候为表中字段指定数据类型的,只有数据符合类型要求才能存储起来,使用数据类型的原则是:够用就行,尽量使用取值范围小的,而不用大的,这样可以更多的节省存储空间。

  • 数值类型

  • 日期和时间类型

  • 字符串类型

3:数值类型

create table if not exists student(

-- 无符号 没有负数

sid int unsigned,

name varchar(20),

gender varchar(20),

age int,

birthday date,

address varchar(20)

);

  • 字符串类型
  • 日期类型

对表结构的其它操作

1:查看当前数据库的所有表

show tables ;

2:查看数据表的创建语句

show create table 数据表名;

3:查看表结构

desc 数据表名;

4:删除表

drop table 数据表表名;

5:修改表结构格式

语法格式

alter table 表名 add 列名 类型(长度) 【约束】;

例子:

为student表添加一个字段为:系别dept类型为department

alter table student add column dept varchar(20);

6:修改列名和类型

语法格式

修改列名和表名,alter table 表名 change 旧列名 新列名 类型(长度) [约束]

为student表中的dept字段更名为department varchar(30)

alter table student change dept department varchar(30);

7:修改表删除列

语法格式:

alter table 表名 drop 列名;

例如:

删除student表中department这列

alter table student drop department;

8:修改表名

语法格式:

rename table 表名 to 新表名;

例如:

将表student改名为stu

rename table student to stu

DML语法基本介绍

DML是指数据操作语言,英文全称是Data Manipulation Language,用来对数据库中标的数据几列进行更新。

关键字:

  • 插入Insert

  • 删除delete

  • 更新update

数据插入

  • 语法格式

insert into 表(列名1,列表2,列表3...) values(值1,值2,值3...);// 向表中插入某些

insert into 表 values(值1,值2,值3...);// 向表中插入所有列
例子:

向表中插入所有列

insert into student(sid, name, gender, age, birthday, address,department)

values (1,'tom','男',18,'2000-01-01','郑州','销售部');

​insert into student values (2,'jerry','女',28,'2001-01-01','北京','研发部');

一次性插入多条数据

insert into student(sid, name, gender, age, birthday, address,department)

values (3,'tom1','男',18,'2000-01-01','郑州','销售部'),

(4,'corky1','女',18,'2001-01-01','北京','法务部');

insert into student values (5,'jerry','女',28,'2001-01-01','北京','研发部'),

(6,'yi','男',39,'2002-01-01','上海','研发部');

数据修改

  • 语法格式

update 表名 set 字段名=值,字段名=值...;

update 表名 set 字段名=值,字段名=值... where 条件;
例子:

将所有学生的地址修改为河南

update student set address='河南';

将id为1的学生的地址修改为河南

update student set address='河南' where id=1;

将id为2的学生的地址修改为北京,成绩修成绩修改为100

update student set address='北京',score=100 where id=2

数据删除

  • 语法格式:

delete from 表名[where 条件];

truncate table 表名 或者truncate 表名
例子:

1:删除sid为3的学生数据

delete from student where sid=3;

2: 删除表所有数据

delete from student;

3:清空表数据

truncate table student;

truncate student;

注意:delete和truncate原理不同,delete只删除内容,而truncate类似于drop table,可以理解为是将整个表删除,然后再创建该表。

实例代码

DQL语法概述

  • 概念

    1. 数据库管理系统一个重要功能就是数据查询,数据查询不应只是简单返回数据库中存储的数据,还应该根据需要对数据库进行筛选以及确定数据以什么样的格式显示。

    2. MySQL提供了功能强大,灵活的语句来实现这些操作。

    3. MySQL数据库使用select语句来查询数据。

  • 应用

基本查询

  • 语法格式

select

[all|distinct]

<目标列的表达式1> [别名],

<目标列的表达式2> [别名]...

from <表名或视图名> [列名],<表名或视图名> [别名]...

[where<条件表达式>]

[group by<列名>

[having<条件表达式>]]

[order by<列名>[asc|desc]]

[limit<数字或者列表>];

  • 简化版语法

select *| 列名 from 表 where 条件

  • 数据准备

创建数据库和表

-- 创建数据库

create database if not exist mydb2;

use mydb2;

-- 创建商品表:

create table product(

pid int primary key auto_increment, -- 商品编号

pname varchar(20) not null , -- 商品名字

price double, -- 商品价格

category_id varchar(20) -- 商品所属分类

);

  • 添加数据

insert into product values(null,'海尔洗衣机',5000,'c001');

insert into product values(null,'美的冰箱',3000,'c001');

insert into product values(null,'格力空调',5000,'c001');

insert into product values(null,'九阳电饭煲',200,'c001');

insert into product values(null,'啄木鸟衬衣',300,'c002');

insert into product values(null,'恒源祥西裤',800,'c002');

insert into product values(null,'花花公子夹克',440,'c002');

insert into product values(null,'劲霸休闲裤',266,'c002');

insert into product values(null,'海澜之家卫衣',180,'c002');

insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'兰蔻面霜',300,'c003');

insert into product values(null,'雅诗兰黛精华水',200,'c003');

insert into product values(null,'香奈儿香水',350,'c003');

insert into product values(null,'SK-II神仙水',350,'c003');

insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');

insert into product values(null,'良品铺子海带丝',17,'c004');

insert into product values(null,'三只松鼠坚果',88,null);

数据准备

1:创建数据库

create database if not exists mydb2;

use mydb2;

2:创建商品表

create table if not exists product(

pid int primary key auto_increment,-- 商品编号

pname varchar(20) ,-- 商品名称

price double, -- 商品价格

category_id int -- 商品所属分类

);

​alter table product modify category_id varchar(20);

3:添加数据

insert into product values(null,'海尔洗衣机',5000,'c001');

insert into product values(null,'美的冰箱',3000,'c001');

insert into product values(null,'格力空调',5000,'c001');

insert into product values(null,'九阳电饭煲',200,'c001');

insert into product values(null,'啄木鸟衬衣',300,'c002');

insert into product values(null,'恒源祥西裤',800,'c002');

insert into product values(null,'花花公子夹克',440,'c002');

insert into product values(null,'劲霸休闲裤',266,'c002');

insert into product values(null,'海澜之家卫衣',180,'c002');

insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'兰蔻面霜',300,'c003');

insert into product values(null,'雅诗兰黛精华水',200,'c003');

insert into product values(null,'香奈儿香水',350,'c003');

insert into product values(null,'SK-II神仙水',350,'c003');

insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');

insert into product values(null,'良品铺子海带丝',17,'c004');

insert into product values(null,'三只松鼠坚果',88,null);

  • 简单查询操作

查询所有的商品

select * from product;

select pid,pname,price,category_id from product;

查询商品名和商品价格

select price,pname from product;

别名查询,使用的关键字是as,(as可以省略的)

表别名

select * from product as p;

select * from product p;

select p.id,u.id from product p,user u;

列表名

select pname as 商品名,price as 商品价格 from product;

去掉重复值

select distinct price from product;

每一行和每一行都不一样

select distinct * from product;

查询结果是表达式(运算查询):将所有商品加价10元进行展示

select pname,price+10 as 新价格,price from product;

运算符

  • 算数运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符

位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数,进行位运算。然后再将计算结果从二进制数变回十进制数。

①:算数运算符

算数运算符

select 6+2;

select 6-2;

select 6*2;

select 6/2;

select 6 div 2;

select 6 mod 2;

将所有商品价格+10元

select price+10 as 价格 from product;

将所有商品的价格上调10%

select price*(1+0.1) as 价格 from product;

条件查询

算数运算符

代码实现:

查询商品名称为"海尔洗衣机"的商品所有信息

select * from product where pname='海尔洗衣机';

查询价格为800商品

select * from product where price=800;

查询价格不是800商品

select * from product where price <>800;

select * from product where price!=800;

select * from product where not (price=800);

查询商品价格大于等于60元的所有商品信息

select * from product where price >=60;

查询商品价格在200到1000之间所有商品

select * from product where price between 200 and 1000;

select * from product where price >= 200 and price<= 1000;

select * from product where price >= 200 && price<= 1000;

查询商品价格在200或800的所有商品

select * from product where price=200 or price=800;

select * from product where price=200 || price=800;

select * from product where price in(200,800);

查询含有'裤'字的所有商品

select * from product where pname like '%裤%'; -- %任意字符

查询以'海'开头的所有商品

select * from product where pname like '海%';

查询第二个字为'蔻'的所有商品

select * from product where pname like '_蔻%';

查询category_id为null的商品

select * from product where category_id is null;

查询category_id不为null的商品

select * from product where category_id is not null;

使用Least求取最小值

select LEAST(10,20,30) as small_number;

select least(10,null,30); -- 如果求最小值有一个null值,不会比较直接null

使用greatest求最大值

select greatest(10,20,30) as big_number ;

select greatest(10,null,30) as big_number ;

如果求最大值有一个null值,不会比较直接null

排序查询

聚合查询

聚合函数--null值得处理

分组查询

分页查询

Insert into Select

相关推荐
缘友一世32 分钟前
macos安装mongodb
数据库·mongodb·macos
万事大吉CC2 小时前
mysql单表查询·3
数据库·mysql
bin91533 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha3 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
一 乐4 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
Java探秘者7 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
2301_786964368 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
苹果醋38 小时前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
阿维的博客日记8 小时前
图文并茂解释水平分表,垂直分表,水平分库,垂直分库
数据库·分库分表
wrx繁星点点9 小时前
事务的四大特性(ACID)
java·开发语言·数据库