本文來(lái)源于MariaDB官方網(wǎng)站绑榴。
1.登錄MariaDB:用戶名替換稱自己的用戶
mysql -u 用戶名 -p -h ip地址 數(shù)據(jù)庫(kù)名字
或者
mysql -u 用戶名 -p
2.創(chuàng)建數(shù)據(jù)庫(kù):
下面的例子哪轿,建立了一個(gè)數(shù)據(jù)庫(kù)test,然后建立了3個(gè)表翔怎。
CREATE DATABASE IF NOT EXISTS test; //創(chuàng)建數(shù)據(jù)庫(kù)
USE test; //使用數(shù)據(jù)庫(kù)test
CREATE TABLE IF NOT EXISTS books ( //創(chuàng)建表books
BookID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100) NOT NULL,
SeriesID INT, AuthorID INT);
CREATE TABLE IF NOT EXISTS authors //創(chuàng)建表authors
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);
CREATE TABLE IF NOT EXISTS series //創(chuàng)建表series
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);
INSERT INTO books (Title,SeriesID,AuthorID) //在books表中插入數(shù)據(jù)
VALUES('The Fellowship of the Ring',1,1),
('The Two Towers',1,1), ('The Return of the King',1,1),
('The Sum of All Men',2,2), ('Brotherhood of the Wolf',2,2),
('Wizardborn',2,2), ('The Hobbbit',0,1);
3.查看一下數(shù)據(jù)庫(kù)test中的所有表:
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| authors |
| books |
| series |
+----------------+
3 rows in set (0.00 sec)
4.獲得一個(gè)表的詳細(xì)信息:
DESCRIBE books;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| BookID | int(11) | NO | PRI | NULL | auto_increment |
| Title | varchar(100) | NO | | NULL | |
| SeriesID | int(11) | YES | | NULL | |
| AuthorID | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
5.檢索一下books表窃诉。
SELECT * FROM books;
+--------+----------------------------+----------+----------+
| BookID | Title | SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
| 1 | The Fellowship of the Ring | 1 | 1 |
| 2 | The Two Towers | 1 | 1 |
| 3 | The Return of the King | 1 | 1 |
| 4 | The Sum of All Men | 2 | 2 |
| 5 | Brotherhood of the Wolf | 2 | 2 |
| 6 | Wizardborn | 2 | 2 |
| 7 | The Hobbbit | 0 | 1 |
+--------+----------------------------+----------+----------+
7 rows in set (0.00 sec)
" * "是選擇所有列。
如果要選擇單獨(dú)的列或者指定的列
比如:
SELECT Title赤套,BookID FROM books;
或者
SELECT Title FROM books;
- 插入數(shù)據(jù)
INSERT INTO books (Title, SeriesID, AuthorID)
VALUES ("Lair of Bones", 2, 2);
Query OK, 1 row affected (0.00 sec)
- 修改數(shù)據(jù)
UPDATE books
SET Title = "The Hobbit" //修改最后一行Hobbbit
WHERE BookID = 7;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0