一、SQL:结构化查询语言
SQL(Structured Query Language)是高级的非过程化变成语言,专门用于查询和修改数据库的信息以及对数据库进行管理和维护的标准语言。SQL语言结构简单,功能强大,得到了众多DBMS的支持。
SQL分为四个部分:
数据定义语言(DDL):用于定义和管理数据对象,包括数据库、数据表等。如create/drop/alter等
数据操作语言(DML):操作数据库对象中包含的数据,如insert/update/delete等
数据查询语言(DQL):查询数据库对象中包含的数据,并返回数据结果,如select
数据控制语言(DCL):管理数据库对象,包括权限管理和数据更改,如grant/revoke/commit/rollback等
二、MySQL的常见操作
1、连接和关闭:mysql –h 【服务器主机地址】-u 用户名 –p 用户密码 quit和exit用于关闭连接
2、创建用户和授权:GRANT 权限 ON 数据库.数据表 TO 用户名@登陆主机 IDENTIFIED BY “密码”
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//添加新用户,名为phper 密码dwqs,在任何主机登陆,对所有数据库拥有查询、插入、修改、删除权限</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//创建用户前,先以root用户登陆</span></span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO phper<span style="color: rgb(0, 96, 128);">@"%"</span> IDENTIFIED BY <span style="color: rgb(0, 96, 128);">"dwqs"</span></span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> </span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//只对mydb数据库创建用户并授权</span></span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> GRANT SELECT,INSERT,UPDATE,DELETE ON mydb.* TO phper@localhost IDENTIFIED BY <span style="color: rgb(0, 96, 128);">"dwqs"</span></span>
3、数据库操作
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//创建名为test的数据库</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> create database [<span style="color: rgb(0, 0, 255);">if</span> not exists] test;</span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//删除test</span></span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> drop database [<span style="color: rgb(0, 0, 255);">if</span> exists] test</span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//显示数据库列表</span></span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> show databases</span>
<span id="lnum7" style="font-size: 14px; color: rgb(96, 96, 96);"> 7:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//使用test数据库</span></span>
<span id="lnum8" style="font-size: 14px; color: rgb(96, 96, 96);"> 8:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 0, 255);">use</span> test</span>
4、数据表操作
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//在test中创建一个表,表名是dwqs</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> create table dwqs </span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> </span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> id <span style="color: rgb(0, 0, 255);">int</span> not <span style="color: rgb(0, 0, 255);">null</span> auto_increment,</span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> name varchar(20) not <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 255);">default</span> <span style="color: rgb(0, 96, 128);">''</span>,</span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> site varchar(20) not <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 255);">default</span> <span style="color: rgb(0, 96, 128);">'www.ido321.com,</span></span>
<span id="lnum7" style="font-size: 14px; color: rgb(96, 96, 96);"> 7:</span><span style="font-size: 14px;"> email varchar(15) not null default ''</span>
<span id="lnum8" style="font-size: 14px; color: rgb(96, 96, 96);"> 8:</span><span style="font-size: 14px;"> ></span>
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//查看在test数据库的中表</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> show tables</span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//查看dwqs表的结构</span></span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> desc dwqs 或 description dwqs</span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//删除dwqs表</span></span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> drop table dwqs</span>
5、数据表的内容管理
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//插入记录</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> insert [into] 表名 [(字段名1),(字段名2),...,(字段名n)] values (<span style="color: rgb(0, 96, 128);">'值1'</span>,<span style="color: rgb(0, 96, 128);">'值2'</span>,...,<span style="color: rgb(0, 96, 128);">'值n'</span>)</span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//更新记录</span></span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> update 表名 set 字段名1=表达式1[,字段名2=表达式2,...,字段名n=表达式n]</span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> [where 条件] [order by 字段] [limit 行数]</span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//删除记录</span></span>
<span id="lnum7" style="font-size: 14px; color: rgb(96, 96, 96);"> 7:</span><span style="font-size: 14px;"> delete from 表名 [where 条件] [order by 字段] [limit 行数]</span>
<span id="lnum8" style="font-size: 14px; color: rgb(96, 96, 96);"> 8:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//查询记录</span></span>
<span id="lnum9" style="font-size: 14px; color: rgb(96, 96, 96);"> 9:</span><span style="font-size: 14px;"> select [all|distinct]</span>
<span id="lnum10" style="font-size: 14px; color: rgb(96, 96, 96);"> 10:</span><span style="font-size: 14px;"> {*|table.*|[table.]field1[AS alias1],[table.]field2[AS alias2][,...]}</span>
<span id="lnum11" style="font-size: 14px; color: rgb(96, 96, 96);"> 11:</span><span style="font-size: 14px;"> from tablaname[,...][in externaldatabase]</span>
<span id="lnum12" style="font-size: 14px; color: rgb(96, 96, 96);"> 12:</span><span style="font-size: 14px;"> [where 条件][group by 字段][having 条件][order by 字段][limit 行数]</span>
说明:在查询中,distinct用于排除重复的数据,只返回一个,默认是all。 as用于给列标记别名: 字段名 as ‘别名’,在查询的结果中,字段名以别名显示.in用于子查询,也可以使用like进行模糊查询,如 like ‘%php%’—>包含php字样的,%表示0个或任意个字符;like ‘dw_s’—>形如dwqs,dwrs等,_表示一个任意字符。group by是对查询结果分组。同时可以再select中嵌入SQL表达式或者函数,如count();
6、修改表
<span id="lnum1" style="font-size: 14px; color: rgb(96, 96, 96);"> 1:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//这个"修改"主要修改表的结构,如添加字段、修改字段属性等</span></span>
<span id="lnum2" style="font-size: 14px; color: rgb(96, 96, 96);"> 2:</span><span style="font-size: 14px;"> alter table 表名 action</span>
<span id="lnum3" style="font-size: 14px; color: rgb(96, 96, 96);"> 3:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//action的具体内容</span></span>
<span id="lnum4" style="font-size: 14px; color: rgb(96, 96, 96);"> 4:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//添加新字段</span></span>
<span id="lnum5" style="font-size: 14px; color: rgb(96, 96, 96);"> 5:</span><span style="font-size: 14px;"> alter table 表名 add 字段名 描述 [first|after 列名]</span>
<span id="lnum6" style="font-size: 14px; color: rgb(96, 96, 96);"> 6:</span><span style="font-size: 14px;"> alter table dwqs add qq <span style="color: rgb(0, 0, 255);">int</span>[14] not <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 128, 0);">//为dwqs表添加一个qq字段,类型是整数,不为空</span></span>
<span id="lnum7" style="font-size: 14px; color: rgb(96, 96, 96);"> 7:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//修改字段</span></span>
<span id="lnum8" style="font-size: 14px; color: rgb(96, 96, 96);"> 8:</span><span style="font-size: 14px;"> alter table 表名 change(modify) 列表 描述</span>
<span id="lnum9" style="font-size: 14px; color: rgb(96, 96, 96);"> 9:</span><span style="font-size: 14px;"> alter table dwqs change qq qq varchar(15) <span style="color: rgb(0, 0, 255);">default</span> 0 <span style="color: rgb(0, 128, 0);">//将qq字段的类型改为varchar,默认值是0</span></span>
<span id="lnum10" style="font-size: 14px; color: rgb(96, 96, 96);"> 10:</span><span style="font-size: 14px;"> alter table dwqs modify qq varchar(15) <span style="color: rgb(0, 0, 255);">default</span> 0 <span style="color: rgb(0, 128, 0);">//将qq字段的类型改为varchar,默认值是0</span></span>
<span id="lnum11" style="font-size: 14px; color: rgb(96, 96, 96);"> 11:</span><span style="font-size: 14px;"> <span style="color: rgb(0, 128, 0);">//修改表名</span></span>
<span id="lnum12" style="font-size: 14px; color: rgb(96, 96, 96);"> 12:</span><span style="font-size: 14px;"> alter table 旧表名 rename <span style="color: rgb(0, 0, 255);">as</span> 新表名</span>
说明:[first|after 列名]表名新字段的位置,first将字段插入第一列,after 列名将字段插入到指定列名之后。未指定该值,默认在末尾添加。
modify和change的区别:change除了更改类型,还会更改列名,所有qq字段出现了两次,前一个是旧列名,后一个是新列名。而modify 没有修改列名的功能,所有qq只出现了一次

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

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

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3汉化版
中文版,非常好用