MySQL 是目前最流行、最知名、最廣泛使用的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)。在全球的網(wǎng)站和應(yīng)用程序中使用 MySQL 的數(shù)量非常多。下面是 MySQL 數(shù)據(jù)庫常見例子:
1. 連接數(shù)據(jù)庫:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢測連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
echo "連接成功";
2. 創(chuàng)建表:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
3. 插入數(shù)據(jù):
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
VALUES (1, 'Doe', 'John', '123 Main St', 'Anytown');
4. 查詢數(shù)據(jù):
SELECT * FROM Persons;
5. 更新數(shù)據(jù):
UPDATE Persons SET LastName='Smith' WHERE PersonID=1;
6. 刪除數(shù)據(jù):
DELETE FROM Persons WHERE PersonID=1;
7. 導(dǎo)出數(shù)據(jù):
SELECT * INTO OUTFILE '/tmp/persons.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM Persons;
8. 導(dǎo)入數(shù)據(jù):
LOAD DATA INFILE '/tmp/persons.csv'
INTO TABLE Persons
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
9. 外鍵關(guān)聯(lián):
CREATE TABLE Orders (
OrderID int,
OrderNumber int,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT fk_PersonID
FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
MySQL 數(shù)據(jù)庫功能非常強(qiáng)大,使用方便,而上面這些例子只是冰山一角。如果你要快速學(xué)習(xí) MySQL 數(shù)據(jù)庫,可以從這些基本操作入手,逐漸掌握更高級(jí)的技巧。