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

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

蘋果公司最新發布的iOS18、iPadOS18以及macOSSequoia系統為Photos應用程式增添了一項重要功能,旨在幫助用戶輕鬆恢復因各種原因遺失或損壞的照片和影片。這項新功能在Photos應用的"工具"部分引入了一個名為"已恢復"的相冊,當用戶設備中存在未納入其照片庫的圖片或影片時,該相冊將自動顯示。 "已恢復"相簿的出現為因資料庫損壞、相機應用未正確保存至照片庫或第三方應用管理照片庫時照片和視頻丟失提供了解決方案。使用者只需簡單幾步

Hibernate多態映射可映射繼承類別到資料庫,提供以下映射類型:joined-subclass:為子類別建立單獨表,包含父類別所有欄位。 table-per-class:為子類別建立單獨資料表,僅包含子類別特有列。 union-subclass:類似joined-subclass,但父類別表聯合所有子類別列。

如何在PHP中使用MySQLi建立資料庫連線:包含MySQLi擴充(require_once)建立連線函數(functionconnect_to_db)呼叫連線函數($conn=connect_to_db())執行查詢($result=$conn->query())關閉連線( $conn->close())

PHP處理資料庫連線報錯,可以使用下列步驟:使用mysqli_connect_errno()取得錯誤代碼。使用mysqli_connect_error()取得錯誤訊息。透過擷取並記錄這些錯誤訊息,可以輕鬆識別並解決資料庫連接問題,確保應用程式的順暢運作。

在Golang中使用資料庫回呼函數可以實現:在指定資料庫操作完成後執行自訂程式碼。透過單獨的函數新增自訂行為,無需編寫額外程式碼。回調函數可用於插入、更新、刪除和查詢操作。必須使用sql.Exec、sql.QueryRow或sql.Query函數才能使用回呼函數。

可以透過使用gjson函式庫或json.Unmarshal函數將JSON資料儲存到MySQL資料庫中。 gjson函式庫提供了方便的方法來解析JSON字段,而json.Unmarshal函數需要一個目標類型指標來解組JSON資料。這兩種方法都需要準備SQL語句和執行插入操作來將資料持久化到資料庫中。

透過Go標準庫database/sql包,可以連接到MySQL、PostgreSQL或SQLite等遠端資料庫:建立包含資料庫連接資訊的連接字串。使用sql.Open()函數開啟資料庫連線。執行SQL查詢和插入操作等資料庫操作。使用defer關閉資料庫連線以釋放資源。

PHP連接資料庫指南:MySQL:安裝MySQLi擴展,建立連線(servername、username、password、dbname)。 PostgreSQL:安裝PgSQL擴展,建立連線(host、dbname、user、password)。 Oracle:安裝OracleOCI8擴展,建立連線(servername、username、password)。實戰案例:取得MySQL資料、PostgreSQL查詢、OracleOCI8更新記錄。
