搜尋
首頁資料庫mysql教程PHP的MySQL扩展:MySQL数据库概述_MySQL

一、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只出现了一次

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何向新的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

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.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)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。

mysql blob:有什麼限制嗎?mysql blob:有什麼限制嗎?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中間佈洛布(16,777,215個比例),andlongblob(4,294,967,967,295 bytes).tousebl觀察:1)考慮pperformance impactsandSandStorLageBlobSextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

MySQL:自動化用戶創建的最佳工具是什麼?MySQL:自動化用戶創建的最佳工具是什麼?May 08, 2025 am 12:22 AM

自動化在MySQL中創建用戶的最佳工具和技術包括:1.MySQLWorkbench,適用於小型到中型環境,易於使用但資源消耗大;2.Ansible,適用於多服務器環境,簡單但學習曲線陡峭;3.自定義Python腳本,靈活但需確保腳本安全性;4.Puppet和Chef,適用於大規模環境,複雜但可擴展。選擇時需考慮規模、學習曲線和集成需求。

mysql:我可以在斑點內搜索嗎?mysql:我可以在斑點內搜索嗎?May 08, 2025 am 12:20 AM

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)轉換theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchUsiseLike.2)forCompresseBlysBlobs,useuncompresseblobs,useuncompressbeforeconversion.3)expperformance impperformance imptactSandDataEcoding.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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境