「所有的人都告訴我,打造出關聯式資料庫是一個瘋狂的想法」
~ by Larry Ellison ~
MySQL的背景參考
因為十年前這瘋狂的想法,所以現在十年後地球的另一端有個人正瘋狂地想瞭解它
長期習慣了圖形介面的操作,接下來即將進入一段枯燥的流程。在資料海中找尋時,一段話突然跳出來:「資料庫是軟體從業人員的基本功」。正因為這段枯燥的流程,得以好好地瞭解資料庫、當年商場創世紀的腥風血雨、 MariaDB背後的感人故事,並將它納入基本功。
C:>mysql -u 使用者帳號 -p密碼
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.13-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
MariaDB [example]> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.1.13-MariaDB |
+-----------------+
1 row in set (0.00 sec)
MariaDB [example]> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
MariaDB [example]>SHOW ENGINES \G
接下來是兩大主角:庫和表
create dateabase 名稱
use database 名稱
drop database 名稱
MariaDB [example]> create table users(name varchar(10), join_date date);
Query OK, 0 rows affected (0.06 sec)
MariaDB [example]> DESC users;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| join_date | date | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec)
MariaDB [example]> SHOW CREATE TABLE users \G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`name` varchar(10) DEFAULT NULL,
`join_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
show
可以查出最初建立資料的過程MariaDB [example]> DROP table users;
Query OK, 0 rows affected (0.02 sec)
MariaDB [example]> DELETE FROM users;
Query OK, 3 rows affected (0.02 sec)
MariaDB [example]> ALTER TABLE users MODIFY name varchar(20);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [example]> DESC users;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| join_date | date | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
MariaDB [cy]> desc member2;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| account | varchar(100) | YES | | NULL | |
| passwd | varchar(255) | YES | | NULL | |
| realname | varchar(100) | YES | | NULL | |
| icon | blob | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.024 sec)
MariaDB [cy]> alter table member2 modify column account varchar(100) not null;
Query OK, 0 rows affected (0.826 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [cy]> desc member2;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| account | varchar(100) | NO | | NULL | |
| passwd | varchar(255) | YES | | NULL | |
| realname | varchar(100) | YES | | NULL | |
| icon | blob | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.015 sec)
MariaDB [cy]> desc member2;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| account | varchar(100) | NO | | NULL | |
| passwd | varchar(255) | YES | | NULL | |
| realname | varchar(100) | YES | | NULL | |
| icon | blob | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.015 sec)
MariaDB [cy]> alter table member2 modify id int auto_increment;
Query OK, 0 rows affected (0.999 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [cy]> desc member2;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account | varchar(100) | NO | | NULL | |
| passwd | varchar(255) | YES | | NULL | |
| realname | varchar(100) | YES | | NULL | |
| icon | blob | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.017 sec)
modify
後面的 int 很容易忘記,需要經過幾次報錯訊息的提醒,才會比較熟悉。MariaDB [example]> ALTER TABLE users CHANGE name names varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
change
用來修改欄位名稱MariaDB [example]> ALTER TABLE users RENAME user;
Query OK, 0 rows affected (0.02 sec)
rename
用來修改表名稱MariaDB [example]> INSERT INTO user (`names`, `join_date`) VALUES('adam','2018-01-01');
Query OK, 1 row affected (0.00 sec)
MariaDB [example]> SELECT * FROM user;
+-------+------------+
| names | join_date |
+-------+------------+
| adam | 2018-01-01 |
+-------+------------+
1 row in set (0.00 sec)
MariaDB [example]> UPDATE user SET names='Adam', join_date='2018-02-01' WHERE names='adam';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [example]> DELETE FROM user WHERE names='Adam';
Query OK, 1 row affected (0.00 sec)
最後讓我們再複習一次基本指令
mysql -u root -p;
exit
show databases;
create database database_name;
use database_name;
select database();
show tables;
create table table_name(id int, cname varchar(100), tel varchar(20), birthday date);
desc cust
select * from table_name;
create table tb1 ( id int primary key auto_increment,
-> f1 varchar(10));
show variables\G
mysql -u root -p dbname < XXX.sql
create table bk1 select * from students;
select cid myid, cname myname, cphone mypnone, csex mysex from bk1 order by csex;
alter table bk1 add score int default 0;
select * from XXX
。操作資料庫的指令下得越明確越好,因為現在是使用範例資料庫所以無感,如果是萬筆的資料庫可能就可以稍微體會到差別了。So far so good! Gear up! 準備遠征吧!