MySql技術內幕之MySQL入門(1)

MySql技術內幕之MySQL入門(1)

安裝

檢查系統中是否已經安裝了MySQLsql

sudo netstat -tap | grep mysql

若沒有顯示已安裝結果,則沒有安裝。不然表示已經安裝。數據庫

sudo apt-get install mysql-server mysql-client

安裝過程當中會讓輸入密碼,記得把密碼記住。ubuntu

關於註釋

三種寫法:ide

  • 用#單行註釋
  • 用-- 單行註釋,注意後面有一空格
  • /* */ 多行註釋
mysql> SELECT 1+1;     # 這個註釋直到該行結束
mysql> SELECT 1+1;     -- 這個註釋直到該行結束
mysql> SELECT 1  /* 這是一個在行中間的註釋 */ + 1;
mysql> SELECT 1+
/*
這是一個
多行註釋的形式
*/
1;

執行SQL語句

ltq@lab:~$ mysql -u root -p  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT NOW();  # 查詢當前日期和時間,使用分號終止語句
+---------------------+
| NOW()               |
+---------------------+
| 2017-10-20 22:07:14 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT NOW()\g  # 查詢當前日期和時間,使用\g終止語句
+---------------------+
| NOW()               |
+---------------------+
| 2017-10-20 22:07:56 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT NOW(), USER(), VERSION()\g  # 查詢時間,用戶,系統版本,並在一列展現
+---------------------+----------------+-------------------------+
| NOW()               | USER()         | VERSION()               |
+---------------------+----------------+-------------------------+
| 2017-10-20 22:08:28 | root@localhost | 5.7.19-0ubuntu0.16.04.1 |
+---------------------+----------------+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT NOW(), USER(), VERSION()\G  #查詢時間,用戶,系統版本,並在一行展現
*************************** 1. row ***************************
    NOW(): 2017-10-20 22:08:43
   USER(): root@localhost
VERSION(): 5.7.19-0ubuntu0.16.04.1
1 row in set (0.00 sec)
mysql> SELECT NOW(), 
    -> USER(),
    -> VERSION()\G  # 在多行輸入
*************************** 1. row ***************************
    NOW(): 2017-10-20 22:18:44
   USER(): root@localhost
VERSION(): 5.7.19-0ubuntu0.16.04.1
1 row in set (0.00 sec)
mysql> SELECT NOW(),  
    -> USER(),
    -> \c   # 使用\c表示不執行上述語句
mysql> SELECT NOW(); SELECT USER(); SELECT VERSION();  # 在一行輸入多條語句
+---------------------+
| NOW()               |
+---------------------+
| 2017-10-20 22:21:38 |
+---------------------+
1 row in set (0.00 sec)

+----------------+
| USER()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

+-------------------------+
| VERSION()               |
+-------------------------+
| 5.7.19-0ubuntu0.16.04.1 |
+-------------------------+
1 row in set (0.00 sec)

關於命令大小寫

通常用大寫字母表示SQL關鍵字和函數名;
用小寫字母表示數據庫,表和列的名字。函數

建立數據庫

mysql> CREATE DATABASE sampdb;  # 建立數據庫
Query OK, 1 row affected (0.00 sec)

mysql> SELECT DATABASE();  # 查看當前數據庫,結果爲NULL
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> USE sampdb;   # 把sampdb設置爲當前默認選擇的數據庫
Database changed
mysql> SELECT DATABASE(); # 查看當前數據庫,結果爲NULL
+------------+
| DATABASE() |
+------------+
| sampdb     |
+------------+
1 row in set (0.00 sec)
mysql> source create_president.sql  # 若是已經在終端cd到create_president.sql路徑下,那麼則可運行該語句
Query OK, 0 rows affected (0.15 sec)

Query OK, 0 rows affected (0.23 sec)

補充,文件create_president.sql的內容以下:ui

# Create president table for U.S. Historical League

DROP TABLE IF EXISTS president;
#@ _CREATE_TABLE_
CREATE TABLE president
(
  last_name  VARCHAR(15) NOT NULL,
  first_name VARCHAR(15) NOT NULL,
  suffix     VARCHAR(5) NULL,
  city       VARCHAR(20) NOT NULL,
  state      VARCHAR(2) NOT NULL,
  birth      DATE NOT NULL,
  death      DATE NULL
);
#@ _CREATE_TABLE_

查看錶的信息

mysql> DESCRIBE president;  # 查看president表的結構
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
| suffix     | varchar(5)  | YES  |     | NULL    |       |
| city       | varchar(20) | NO   |     | NULL    |       |
| state      | varchar(2)  | NO   |     | NULL    |       |
| birth      | date        | NO   |     | NULL    |       |
| death      | date        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> DESC president; # DESCRIBE的簡寫爲DESC
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
| suffix     | varchar(5)  | YES  |     | NULL    |       |
| city       | varchar(20) | NO   |     | NULL    |       |
| state      | varchar(2)  | NO   |     | NULL    |       |
| birth      | date        | NO   |     | NULL    |       |
| death      | date        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> EXPLAIN president;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
| suffix     | varchar(5)  | YES  |     | NULL    |       |
| city       | varchar(20) | NO   |     | NULL    |       |
| state      | varchar(2)  | NO   |     | NULL    |       |
| birth      | date        | NO   |     | NULL    |       |
| death      | date        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM president;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
| suffix     | varchar(5)  | YES  |     | NULL    |       |
| city       | varchar(20) | NO   |     | NULL    |       |
| state      | varchar(2)  | NO   |     | NULL    |       |
| birth      | date        | NO   |     | NULL    |       |
| death      | date        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> SHOW FIELDS FROM president;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
| suffix     | varchar(5)  | YES  |     | NULL    |       |
| city       | varchar(20) | NO   |     | NULL    |       |
| state      | varchar(2)  | NO   |     | NULL    |       |
| birth      | date        | NO   |     | NULL    |       |
| death      | date        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

查看更加詳細的信息

mysql> SHOW FULL COLUMNS FROM president;  # SHOW FULL COLUMNS 比 SHOW COLUMNS顯示更多的信息
+------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| Field      | Type        | Collation         | Null | Key | Default | Extra | Privileges                      | Comment |
+------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| last_name  | varchar(15) | latin1_swedish_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| first_name | varchar(15) | latin1_swedish_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| suffix     | varchar(5)  | latin1_swedish_ci | YES  |     | NULL    |       | select,insert,update,references |         |
| city       | varchar(20) | latin1_swedish_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| state      | varchar(2)  | latin1_swedish_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| birth      | date        | NULL              | NO   |     | NULL    |       | select,insert,update,references |         |
| death      | date        | NULL              | YES  |     | NULL    |       | select,insert,update,references |         |
+------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
7 rows in set (0.00 sec)

查看與給定模式相匹配的列

若是須要對所查找的信息加以限定,code

mysql> DESCRIBE president '%name';  # 查找名稱中包含name的項
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> SHOW FIELDS FROM president like '%name';  # 查找名稱中包含name的項,另一種寫法
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| last_name  | varchar(15) | NO   |     | NULL    |       |
| first_name | varchar(15) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

插入數據

利用insert添加行

一次添加一行數據:server

INSERT INTO president VALUES ('Washington','George',NULL,'Wakefield','VA','1732-02-22','1799-12-14');
INSERT INTO president VALUES ('Adams','John',NULL,'Braintree','MA','1735-10-30','1826-07-04');

一次添加多行數據:ci

INSERT INTO president VALUES ('Jefferson','Thomas',NULL,'Albemarle County','VA','1743-04-13','1826-07-04'), ('Madison','James',NULL,'Port Conway','VA','1751-03-16','1836-06-28');

利用文件添加新行

mysql> source insert_president.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.06 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.07 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.02 sec)

Query OK, 1 row affected (0.02 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.03 sec)

Query OK, 1 row affected (0.04 sec)

Query OK, 1 row affected (0.03 sec)

補充,文件source insert_president.sql內容以下:

DELETE FROM president;
INSERT INTO president VALUES ('Washington','George',NULL,'Wakefield','VA','1732-02-22','1799-12-14');
INSERT INTO president VALUES ('Adams','John',NULL,'Braintree','MA','1735-10-30','1826-07-04');
INSERT INTO president VALUES ('Jefferson','Thomas',NULL,'Albemarle County','VA','1743-04-13','1826-07-04');
INSERT INTO president VALUES ('Madison','James',NULL,'Port Conway','VA','1751-03-16','1836-06-28');
INSERT INTO president VALUES ('Monroe','James',NULL,'Westmoreland County','VA','1758-04-28','1831-07-04');
INSERT INTO president VALUES ('Adams','John Quincy',NULL,'Braintree','MA','1767-07-11','1848-02-23');
INSERT INTO president VALUES ('Jackson','Andrew',NULL,'Waxhaw settlement','SC','1767-03-15','1845-06-08');
INSERT INTO president VALUES ('Van Buren','Martin',NULL,'Kinderhook','NY','1782-12-05','1862-07-24');
INSERT INTO president VALUES ('Harrison','William H.',NULL,'Berkeley','VA','1773-02-09','1841-04-04');
INSERT INTO president VALUES ('Tyler','John',NULL,'Greenway','VA','1790-03-29','1862-01-18');
INSERT INTO president VALUES ('Polk','James K.',NULL,'Pineville','NC','1795-11-02','1849-06-15');
INSERT INTO president VALUES ('Taylor','Zachary',NULL,'Orange County','VA','1784-11-24','1850-07-09');
INSERT INTO president VALUES ('Fillmore','Millard',NULL,'Locke','NY','1800-01-07','1874-03-08');
INSERT INTO president VALUES ('Pierce','Franklin',NULL,'Hillsboro','NH','1804-11-23','1869-10-08');
INSERT INTO president VALUES ('Buchanan','James',NULL,'Mercersburg','PA','1791-04-23','1868-06-01');
INSERT INTO president VALUES ('Lincoln','Abraham',NULL,'Hodgenville','KY','1809-02-12','1865-04-15');
INSERT INTO president VALUES ('Johnson','Andrew',NULL,'Raleigh','NC','1808-12-29','1875-07-31');
INSERT INTO president VALUES ('Grant','Ulysses S.',NULL,'Point Pleasant','OH','1822-04-27','1885-07-23');
INSERT INTO president VALUES ('Hayes','Rutherford B.',NULL,'Delaware','OH','1822-10-04','1893-01-17');
INSERT INTO president VALUES ('Garfield','James A.',NULL,'Orange','OH','1831-11-19','1881-09-19');
INSERT INTO president VALUES ('Arthur','Chester A.',NULL,'Fairfield','VT','1829-10-05','1886-11-18');
INSERT INTO president VALUES ('Cleveland','Grover',NULL,'Caldwell','NJ','1837-03-18','1908-06-24');
INSERT INTO president VALUES ('Harrison','Benjamin',NULL,'North Bend','OH','1833-08-20','1901-03-13');
INSERT INTO president VALUES ('McKinley','William',NULL,'Niles','OH','1843-01-29','1901-09-14');
INSERT INTO president VALUES ('Roosevelt','Theodore',NULL,'New York','NY','1858-10-27','1919-01-06');
INSERT INTO president VALUES ('Taft','William H.',NULL,'Cincinnati','OH','1857-09-15','1930-03-08');
INSERT INTO president VALUES ('Wilson','Woodrow',NULL,'Staunton','VA','1856-12-19','1924-02-03');
INSERT INTO president VALUES ('Harding','Warren G.',NULL,'Blooming Grove','OH','1865-11-02','1923-08-02');
INSERT INTO president VALUES ('Coolidge','Calvin',NULL,'Plymouth Notch','VT','1872-07-04','1933-01-05');
INSERT INTO president VALUES ('Hoover','Herbert C.',NULL,'West Branch','IA','1874-08-10','1964-10-20');
INSERT INTO president VALUES ('Roosevelt','Franklin D.',NULL,'Hyde Park','NY','1882-01-30','1945-04-12');
INSERT INTO president VALUES ('Truman','Harry S',NULL,'Lamar','MO','1884-05-08','1972-12-26');
INSERT INTO president VALUES ('Eisenhower','Dwight D.',NULL,'Denison','TX','1890-10-14','1969-03-28');
INSERT INTO president VALUES ('Kennedy','John F.',NULL,'Brookline','MA','1917-05-29','1963-11-22');
INSERT INTO president VALUES ('Johnson','Lyndon B.',NULL,'Stonewall','TX','1908-08-27','1973-01-22');
INSERT INTO president VALUES ('Nixon','Richard M.',NULL,'Yorba Linda','CA','1913-01-09','1994-04-22');
INSERT INTO president VALUES ('Ford','Gerald R.',NULL,'Omaha','NE','1913-07-14','2006-12-26');
INSERT INTO president VALUES ('Carter','James E.','Jr.','Plains','GA','1924-10-01',NULL);
INSERT INTO president VALUES ('Reagan','Ronald W.',NULL,'Tampico','IL','1911-02-06','2004-06-05');
INSERT INTO president VALUES ('Bush','George H.W.',NULL,'Milton','MA','1924-06-12',NULL);
INSERT INTO president VALUES ('Clinton','William J.',NULL,'Hope','AR','1946-08-19',NULL);
INSERT INTO president VALUES ('Bush','George W.',NULL,'New Haven','CT','1946-07-06',NULL);
INSERT INTO president VALUES ('Obama','Barack H.',NULL,'Honolulu','HI','1961-08-04',NULL);

總結

登陸

mysql -u root -p

本節SQL語句

mysql> SELECT NOW();  # 查詢當前日期和時間,使用分號終止語句
mysql> SELECT NOW()\g  # 查詢當前日期和時間,使用\g終止語句
mysql> SELECT NOW(), USER(), VERSION()\g  # 查詢時間,用戶,系統版本,並在一列展現
mysql> SELECT NOW(), USER(), VERSION()\G  #查詢時間,用戶,系統版本,並在一行展現
mysql> SELECT NOW(); SELECT USER(); SELECT VERSION();  # 在一行輸入多條語句
mysql> CREATE DATABASE sampdb;  # 建立數據庫
mysql> SELECT DATABASE();  # 查看當前數據庫,結果爲NULL
mysql> USE sampdb;   # 把sampdb設置爲當前默認選擇的數據庫
mysql> SELECT DATABASE(); # 查看當前數據庫,結果爲sampdb
mysql> source create_president.sql  # 若是已經在終端cd到create_president.sql路徑下,那麼則可運行該語句
mysql> DESCRIBE president;  # 查看president表的結構
mysql> DESC president; # DESCRIBE的簡寫爲DESC
mysql> EXPLAIN president;
mysql> SHOW COLUMNS FROM president;
mysql> SHOW FIELDS FROM president;
mysql> SHOW FULL COLUMNS FROM president;  # SHOW FULL COLUMNS 比 SHOW COLUMNS顯示更多的信息
mysql> DESCRIBE president '%name';  # 查找名稱中包含name的項
mysql> SHOW FIELDS FROM president like '%name';  # 查找名稱中包含name的項,另一種寫法
mysql> INSERT INTO president VALUES ('Washington','George',NULL,'Wakefield','VA','1732-02-22','1799-12-14');  # 插入數據
mysql> INSERT INTO president VALUES ('Jefferson','Thomas',NULL,'Albemarle County','VA','1743-04-13','1826-07-04'), ('Madison','James',NULL,'Port Conway','VA','1751-03-16','1836-06-28'); # 一次添加多行數據
mysql> source insert_president.sql  # 利用文件添加新行

待補充……

相關文章
相關標籤/搜索