MySQL基础语法(长期更新)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 8.0MySQL常见语法:#启动mysql服务器:net start mysql#关闭:net stop mysql#命令行进入数据库:mysql -h 主机地址-u 用户名 -p用户密码 ( -p后面的密码要连着一起写) mysql -u r...

8.0MySQL常见语法:

#启动mysql服务器:net start mysql

#关闭:net stop mysql

#命令行进入数据库:

mysql -h 主机地址-u 用户名 -p用户密码 ( -p后面的密码要连着一起写) mysql -u root -ppassword

#退出:exit:

#显示当前mysql的version的各种信息:status;

#显示数据库:show databases;

#判断是否存在数据库test,有的话先删除:drop database if exists test;

#创建数据库:create database test;

#删除数据库:drop database test;

#使用该数据库:use test;

#显示数据库里面的表(先进库 在显示表):show tables;

#先判断表是否存在,存在先删除: drop table if exists student;

#创建表:create table student(

id int auto_increment primary key,

name varchar(50),

sex varchar(20),

date varchar(50),

content varchar(100)

)default charset=utf8;

#删除表:drop table student;

#查看表的结构:describe student;  可以简写为desc student;

#插入数据:

insert into student values(null,'aa','男','1994-05-14','yy');

insert into student values(null,'bb','女','1991-11-25','ll');

insert into student values(null,'cc','男','1992-10-5','tt');

insert into student values(null,'dd','男','1992-09-09','......');

insert into student values(null,'ee','女','1994-08-16','ll');

#查询表中的数据:

select * from student;

select id,name from student;

#修改某一条数据:update student set sex='男' where id=4;

#删除数据:delete from student where id=5;

# and 且:select * from student where date>'1949-10-1' and date<'2049-10-1';

# or 或:select * from student where date<'1992-10-5' or date>'1992-10-5';

#between:select * from student where date between'1992-2-3' and '1992-12-12';

#in 查询指定集合内的数据:select * from student where id in(1,3,5);

#排序语法 asc升序、desc 降序:

升序:select * from student order by id asc; 

降序:select * from student order by id desc; 

#分组查询 #聚合函数

select max(id),name,sex from student group by sex;

select min(date) from student;

select avg(id) as'求平均' from student;

select count(*) from student;  #统计表中总数

select count(sex) from student;  #统计表中性别总数  若有一条数据中sex为空的话,就不予以统计

select sum(id) from student;

#查询第i条以后到第j条的数据(不包括第i条):select * from student limit 2,5; 

 #显示3-5条数据

#查看数据文件路径:

mysql的数据文件在datadir下,在数据库中执行 show variables like '%datadir%';会显示数据库存放路径

#巩固练习:

创建表:

create table person(

id int  auto_increment primary key,

name varchar(10)not null,

sex varchar(50) ,

phone varchar(13),

account varchar(18),

password varchar(20) 

)default charset=utf8;


create table c(

id int auto_increment primary key,

name varchar(10)notnull,

sex varchar(50) ,  #DEFAULT'男' ,

age int unsigned, #不能为负值(如为负值 则默认为0)

sno int unique    #不可重复

);

drop table c;

desc c;

insert into c (id,name,sex,age,sno)values(null,'涛哥','男',68,1);

insert into c (id,name,sex,age,sno)values(null,'aa','男',68,2);

insert into c (id,name,sex,age,sno)values(null,'平平','男',35,3);

...

select * from c;

#修改数据update c setage=66 where id=2;update c setname='花花',age=21,sex='女'where id=2 delete from c where age=21;

#常用查询语句:

select name,age ,id from c select * from c where age>40 and age<60;  # and 

select * from c where age<40 or age<60;  #or

select * from c where age between 40 and 60 #between 

select * from c where age in(30,48,68,99);    #in

 查询指定集合内的数据select * from c order by age desc;      #order by (asc升序 desc降序)

#分组查询select name,max(age)from c groupby sex;  #按性别分组查年龄最大值

#聚合函数selec tmin(age) from c;select avg(age) as'平均年龄 'from c;select count(*) from c;  #统计表中数据总数select sum(age)from c;

#修改表的名字

语法格式:alter table tbl_name rename to new_name 

例如: alter table c rename to a

#表结构修改create table test

(

id int not null auto_increment primary key, #设定主键

name varchar(20)not null default'NoName', #设定默认值

department_id int not null,

position_id int not null,unique (department_id,position_id) #设定唯一值

);

#向表中增加一个字段(列) 语法格式:

 alter table tablename add column name type;/alter table tablename add(columnname type);

alter table test add column eat varchar(20);   这里的 eat 代表的是新字段


#修改某个表的字段名称及指定为空或非空:

alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空];


#修改某个表的字段类型及指定为空或非空:

alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];

alter table 表名称 modify 字段名称 字段类型 [是否允许非空];


#student表

img_adfebec48505ec0d0d51a6cc1e2b693b.png
student

#coder表

img_fd5c9435211453df50db758e0767a848.png
coder表

为了看到多表关联的效果,我将student表的 year 字段改成 date 字段:

alert table student change year date date;

表字段修改完毕以后然后插入数据:

insert into student values(null,'dd','嘿嘿咻咻','中山大道','1992-09-09','男');

insert into student values(null,'dd','雅蠛蝶','方圆E时光','1988-10-2','女');

insert into student values(null,'dd','用力呀宝宝','越秀公园','1989-03-6','男');

insert into student values(null,'dd','用力呀宝宝','越秀公园','1989-03-06','男');

最终的student表为:

img_062181fa62b1fc2aafc0f97cc8bed038.png
student

 多表关联: 

UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。(默认情况下 UNION 操作符已经删除了重复数据)

下面的 SQL 语句从 "coder" 和 "student" 表中选取所有不同的date(只有不同的值):按照升序对结果进行统一排序:

 select date from student union select date from coder order by date;

img_1efb45cbc89372bdb6cc5e9cc8e7d7aa.png

下面的 SQL 语句从 "coder" 和 "student" 表中选取所有的date(含重复值 使用 union all):

select date from student union all select date from coder order by date;

img_3ce2872af12b5eabf30e103ac15ab5be.png

JOIN关键字:

INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。

RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

现在有两张表 一个是 coder表 ;一个是student表;两张表数据如下:

img_075298cd0fdeeda444e4d1f7511bdf59.png

JOIN语法格式:

select a.id, a.name, b.date FROM coder a  JOIN student b ON a.name = b.name;

这里的a 代表的就是 coder表:这里的b 代表的就是 student表,结果如下:

img_5917e36f2732e867c6faddd109039713.png
Join

其中LEFT JOIN 以及RIGHT JOIN 关键字,上面也说到了,分别获取左、右两边所有记录:

SELECT a.id, a.name, b.date FROM coder a  LEFT JOIN student b ON a.name = b.name;

img_b16c08c5271ae459fb0cbe4e6f08dcdb.png
LEFT JOIN

SELECT a.id, a.name, b.date FROM coder a  RIGHT JOIN student b ON a.name = b.name;

img_71eb6aea480d64504dbc7d0bc1bbe1da.png
RIGHT JOIN

NULL 值处理

MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作。

所以为了处理这种情况,MySQL提供了三大运算符:

IS NULL: 当列的值是 NULL,此运算符返回 true。

IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。

<=>: 比较操作符(不同于=运算符),当比较的的两个值为 NULL 时返回 true。

关于 NULL 的条件比较运算是比较特殊的。我们不能直接使用 = NULL 或 != NULL 这种Java的语法在表中查找 NULL 值 。在 MySQL 中,NULL 值与任何其它值的比较(即使是 NULL)永远返回 false,即 NULL = NULL 返回false 。

因此MySQL 中处理 NULL 的 正确使用姿势是 使用 IS NULL 和 IS NOT NULL 运算符。

img_550b24890357555f57528036eba28f94.png
NULL运算符

正则表达式:

MySQL中使用 REGEXP 操作符来进行正则表达式匹配。假定现有login表 正则表达式MYSQL语法格式如下:

img_445f11b0e372dadcf11be44490a6cdaf.png
login

查找name字段中以'小'为开头的所有数据(使用 ^ 这个符号):

SELECT name FROM login WHERE name REGEXP '^小';

查找name字段中以'王'为结尾的所有数据(使用 $ 这个符号):

SELECT name FROM login WHERE name REGEXP '王$';

查找date字段中包含'小明'字符串的所有数据:

SELECT name FROM login WHERE name REGEXP '小明';

img_fa29c7ee1c33499e8b41a1244246cbc5.png
select语句


#用文本方式将数据装入数据库表中(例如D:/mysql.txt)loaddata local infile "D:/mysql.txt"intotable MYTABLE;

#导入.sql文件命令(例如D:/mysql.sql)

source d:/mysql.sql;  #或者/. d:/mysql.sql;


如果这篇文章对你有帮助,希望各位看官留下宝贵的star,谢谢。

Ps:著作权归作者所有,转载请注明作者, 商业转载请联系作者获得授权,非商业转载请注明出处(开头或结尾请添加转载出处,添加原文url地址),文章请勿滥用,也希望大家尊重笔者的劳动成果。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
关系型数据库 MySQL
Mysql常用语法总结
Mysql常用语法总结
22 0
|
1月前
|
SQL 关系型数据库 MySQL
MySQL 数据库基本语法
SQL,全称Structured Query Language(结构化查询语言),是一种用于管理关系型数据库(RDBMS)的编程语言。SQL用于创建、修改、查询和删除数据库中的数据,以及定义数据库架构。它是数据库管理系统(DBMS)与应用程序之间的标准通信协议。
77 6
|
6月前
|
SQL 关系型数据库 MySQL
MySQL数据的基础语法
MySQL数据的基础语法
|
6月前
|
存储 关系型数据库 MySQL
【ChatGPT】输出MySQL常用语法汇总
【ChatGPT】输出MySQL常用语法汇总
|
6月前
|
关系型数据库 MySQL
【mysql】快速使用mysql exists 语法
【mysql】快速使用mysql exists 语法
47 1
|
2月前
|
关系型数据库 MySQL
mysql一些常用语法
mysql一些常用语法
|
2月前
|
存储 关系型数据库 MySQL
7、Mysql语法
7、Mysql语法
21 0
|
2月前
|
存储 关系型数据库 MySQL
小课堂 -- Mysql语法
小课堂 -- Mysql语法
16 0
|
2月前
|
SQL 存储 关系型数据库
mysql语法
mysql语法
28 2
|
2月前
|
SQL 关系型数据库 MySQL
like concat 兼容h2、mysql、pgsql语法
like concat 兼容h2、mysql、pgsql语法
34 0