搜索
首页数据库mysql教程Mysql研究之SQL语言的设计与编撰完全解析

Mysql研究之SQL语言的设计与编写完全解析 一、 SQL 语句分类 数据定义语言( DDL ): 用于定义和管理数据对象,包括数据库、数据表、视图、索引等。例如: CREATE 、 DROP 、 ALTER 等语句。 数据操作语言( DML ):【和表中的数据记录有关的语言】 用于操

Mysql研究之SQL语言的设计与编写完全解析

一、SQL语句分类

数据定义语言(DDL):

用于定义和管理数据对象,包括数据库、数据表、视图、索引等。例如:CREATEDROPALTER等语句。

数据操作语言(DML):【和表中的数据记录有关的语言】

用于操作数据库对象中所包含的数据。例如:INSERTUPDATEDELETE语句。

数据查询语言(DQL):

用于查询数据库对象中所包含的数据,能够进行单表查询、连接查询、嵌套查询,以及集合查询等各种复杂程度不同的数据库查询,并将数据返回到客户机中显示。例如:SELECT语句(占60%)。

数据控制语言(DCL):

是用来管理数据库的语言,包含管理权限及数据更改。例如:GRANTREVOKECOMMITROLLBACK等语句。

二、SQL语句应用案例

1DDL【可以按照以下格式写入一个文件,然后再粘贴到MySQL数据库中】

createtable if not exists cats (

idint not null auto_increment,

pidint not null default ’0′,

namevarchar(30) not null default ”,

desntext not null default ”,

primarykey(id),

indexname(name,pid)

);

createtable if not exists products(

idint not null auto_increment,

cidint not null default 0,

namevarchar(60) not null default ”,

pricedouble (7,2) not null default 0.00,

numint not null default 0,

desntext,

ptimeint not null default 0,

primarykey(id),

keypname(name,price)

);

2DML

ainsert,插入表数据

insertinto 表名([字段列表])values(值列表),(值列表2),(值列表3),…,(值列表n);

特点:

1.如果在表名后没有给出字段列表,则值列表必须填充所有字段的值,必须按表中默认的顺序插入

2.所有需要写字段名的地方都不加单引号或双引号,但是建议所有值都要以字符形式使用

3.建议在插入数据时,最好给出字段列表,则值只要和字段列表一一对应即可,可以不按表中字段的顺序

?

bupdate表名 set字段=’‘[,字段2='2',...,字段n='n'][条件]#条件指定需要更改的记录

e.g. updatecats set pid=’3′ where id=’1′;

updatecats set pid=’99′ where id >= ’1′ && id

cdeletefrom 表名 [条件]

deletefrom cats; #清空数据表

truncatecats; #也可以清空数据表,效率更高,truncate截断

?

dwhere条件

无论更新、删除、查找,只要写对条件就能准确找到要管理的一条或多条数据

【都可以使用各种运算符号,可以把字段当作一个变量来使用】

3DQLselect

SELECT[ALL | DISTINCT]

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]}

FROM 表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!

SELECT[ALL | DISTINCT] #distinct 明显的,清楚的,有区别的

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]} #alias 别名,化名

FROM 表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!

?

1、字段要列出要查询的字段

e.g. selectname,price from products;

selectprice,name from products;

select* from products;

selectproducts.* from products; #单表其实不需要使用表名

?

2、可以为每个字段起个别名【后面会用到(关键字,多表查询)】【表也可起别名(多表查询)】

e.g. selectname as bookname,price as bookprice from products;#使用别名;也可不加as;注意别名中有空格时,需要加单引号;

?

3、使用distinct作用与整个记录,取消重复的数据,只返回一个,而不是单独的一列

e.g. selectdistinct price ‘book price’ from products;

?

4、在SQL语句中使用表达式的列(可以使用算术运算符,条件运算符,逻辑运算符

游戏编程网www.cgzhw.com有详细的说明,这里就不再重复了。

5WHERE可以在SELECT/UPDATE/DELETE

a)可使用的逻辑运算符号(将多个条件组合)

&&/AND ||/OR !/NOT

b)可使用的比较运算符号

=#判断是否相等,与程序中的==作用相同

#判断是否相等,与=一致,但可以用于与NULL比较

!=/ #不等号

?

>

>=

c)程序中没有的运算符

ISNULL #‘NULL’ 相等

ISNOT NULL

?

BETWEENAND

e.g. select* from products where id between 10 and 20;

“select* from products where id >= 10 && id 作用相同

?

NOTBETWEEN AND

?

IN

e.g. select* from products where id in(5,10,15,20);

updateproducts set num = 77 where id in(5,10,15,20);

deletefrom products where id in(5,10);

?

d)模糊查询

LIKE _(任意一个字符)和%0个或多个任意字符)两个通配符号

e.g. select* from products where name like ‘______’; #查找任意名字为6个字符的数据

select* from products where name like ‘%java%’; #查询名字中包含有java的数据

?

NOTLIKE

e.g. select* from products where name not like ‘%java%’; #查询名字中不包含java字样的数据。

?

REGEXP/RLIKE【正则表达式】 #RegExp 正则表达式

e.g. select* from products where name regexp ‘^java’; #查找所有以java开头的数据

select* from products where name regexp ‘s$’; #查找所有以s结尾的数据

?

6、多表查询(连接查询),比较常用 #ambiguous

e.g. selectcats.name,products.name from cats,products;

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p;#A表中的记录与B表中的记录依次匹配,得到A*B种结果【笛卡尔乘积】,该结果是没有意义的。

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid;

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid and c.id=3;

selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,catsb; #将单表分为多表,进行查询

selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b wherea.pid = b.id;

?

7、嵌套查询子查询

e.g. select* from products where cid in(select id from carts where name regexp’^j’) ;

select* from products where cid in(select id from carts where name like’j%’); #作用相同

?

8orderby 字段 [asc正序]desc倒序

e.g. select* from order by name;

select* from order by price; #按价格非递减排序

select* from order by price desc; #非递增排序

select* from where cid > 5 order by price desc; #where结合使用

?

9limitcount【限制显示个数】

e.g. select* from limit 7;

select* from order by id desc limit 7;

select* from where id

select* from where id > 14 order by id asc limit 0,1; #limit0,1表示从第0个开始取,取1

?

10groupby 字段【分组】

常用函数:

count() #一个字段的总数

sum()

avg()#平均值

max()

min()

e.g. selectcount(*),sum(price),avg(price),max(price),min(price) from products;

selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid;

selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid having avg(price) > 50; #having条件,与where类似

#having必须与gropby结合才能使用

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL字符串类型:存储,性能和最佳实践MySQL字符串类型:存储,性能和最佳实践May 10, 2025 am 12:02 AM

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)长度,始终使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

了解MySQL字符串类型:VARCHAR,文本,char等了解MySQL字符串类型:VARCHAR,文本,char等May 10, 2025 am 12:02 AM

mySqlStringTypesIncludeVarChar,文本,char,enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptOptoPeptoPepecifientlimit.2)textisidealforlargetStortStorStoverStorextorewiteWithoutAdefinedLengthl.3)charlisfixed-Length

MySQL中的字符串数据类型是什么?MySQL中的字符串数据类型是什么?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

如何向新的MySQL用户授予权限如何向新的MySQL用户授予权限May 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

如何在MySQL中添加用户:逐步指南如何在MySQL中添加用户:逐步指南May 09, 2025 am 12:14 AM

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

mysql:添加具有复杂权限的新用户mysql:添加具有复杂权限的新用户May 09, 2025 am 12:09 AM

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

mysql:字符串数据类型和coltrationsmysql:字符串数据类型和coltrationsMay 09, 2025 am 12:08 AM

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

MySQL:我应该在Varchars上使用什么长度?MySQL:我应该在Varchars上使用什么长度?May 09, 2025 am 12:06 AM

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)