常用MYSQL短语

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 主要总结mysql一些常用知识点[常用命令]1、查看数据库show database;2、创建数据库create database database_name;3、切换数据库use database_name;4、查看某数据库中所有的数据表show table;5、创建数据表View...

主要总结mysql一些常用知识点

[常用命令]

1、查看数据库

show database;
2、创建数据库

create database database_name;
3、切换数据库

use database_name;
4、查看某数据库中所有的数据表

show table;
5、创建数据表

View Code
6、查看数据表结构

describe table_name; --缩写: desc
7、查看数据表中的记录

select * from table_name;

-- 去重复
select distinct name from table_name
8、往数据表中添加数据记录

INSERT INTO table_name
VALUES('puffball','Diane','hanst','f','1999-03-23',NULL);

-- 指定属性
insert into user3 (name) value('asfjl');
9、删除数据

delete from table_name where name='puffball';
10、修改数据

update table_name set name='wang' where owner='haha'
11、建表约束--主键

View Code
12、建表约束--自增

create table user3(

id int primary key auto_increment,
name varchar(20)

);
12、建表约束--唯一:约束修饰的字段的值不可以重复

View Code
13、非空约束:修饰的字段不能为NULL

复制代码
create table user6(

id int,
name varchar(20) not null 

);
-- 反null? 异常
insert into user6 (name) value('jfsl');
复制代码
14、默认约束

复制代码
create table user7(

id int,
name varchar(20),
age int default 10

);

insert into user7 (id,name) value(1,'slfj');
insert into user7 (id,name,age) values(1,'slsfj',5);
复制代码
15、外键约束

复制代码
create table classes(

id int primary key,
name varchar(20)

);
create table students(

id int primary key,
class_id int,
foreign key(class_id) references classes(id)

);
复制代码

[查询]

1、多表查询

复制代码
-- 两表查询
select sname,cno, degree from student,score
where student.sno = score.sno;

-- 三表查询
select sname, cname,degree from student,course,course,score
where student.sno = score.sno
and course.cno = score.cno;
复制代码
2、分组查询

复制代码
-- 子查询加分组求评均
select cno, avg(degree) from score
where sno in (select sno from student where class='1233')
group by cno;

-- year函数与带in关键字的子查询
select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,117));
复制代码
3、多层嵌套查询

View Code
4、union与not in

View Code
5、any与all

复制代码
-- any表示至少一个
select * from score where cno='34'
and degree>any(select degree from score where cno='334')
order by degree desc;

-- all表示所有
select * from score where cno='34'
and degree>all(select degree from score where cno='334')
order by degree desc;
复制代码
原文地址https://www.cnblogs.com/lisen10/p/sql.html

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5月前
|
机器学习/深度学习 SQL 关系型数据库
MySQL 正则表达式
MySQL 正则表达式
|
27天前
|
关系型数据库 MySQL 数据库
MySQL 全文检索
MySQL 全文检索
|
2月前
|
SQL Oracle 关系型数据库
mysql空格问题引起查询问题
mysql空格问题引起查询问题
15 0
|
10月前
|
SQL 关系型数据库 MySQL
【MySQL】一文带你了解检索数据(二)
【MySQL】一文带你了解检索数据(二)
75 0
|
10月前
|
SQL 关系型数据库 MySQL
【MySQL】一文带你了解检索数据(一)
【MySQL】一文带你了解检索数据(一)
61 0
【MySQL】一文带你了解检索数据(一)
|
11月前
|
SQL 自然语言处理 搜索推荐
mysql全文搜索
mysql全文搜索
76 2
|
11月前
|
存储 关系型数据库 MySQL
MySQL长文本字段的选
MySQL长文本字段的选
85 0
MySQL长文本字段的选
|
11月前
|
关系型数据库 MySQL
Mysql文本函数
Mysql文本函数
92 1
|
SQL 算法 关系型数据库
MySQL中索引与算法
该文章所用到的表结构
83 0
MySQL中索引与算法
|
算法 关系型数据库 MySQL
正则表达式:(mysql)
正则表达式:(mysql)
107 0
正则表达式:(mysql)