mysql技术内幕sampdb_MySql技术内幕之MySQL入门(1)
MySql技術(shù)內(nèi)幕之MySQL入門(1)
安裝
檢查系統(tǒng)中是否已經(jīng)安裝了MySQL
sudo netstat -tap | grep mysql
若沒(méi)有顯示已安裝結(jié)果,則沒(méi)有安裝。否則表示已經(jīng)安裝。
sudo apt-get install mysql-server mysql-client
安裝過(guò)程中會(huì)讓輸入密碼,記得把密碼記住。
關(guān)于注釋
三種寫法:
用#單行注釋
用-- 單行注釋,注意后面有一空格
/* */ 多行注釋
mysql> SELECT 1+1; # 這個(gè)注釋直到該行結(jié)束
mysql> SELECT 1+1; -- 這個(gè)注釋直到該行結(jié)束
mysql> SELECT 1 /* 這是一個(gè)在行中間的注釋 */ + 1;
mysql> SELECT 1+
/*
這是一個(gè)
多行注釋的形式
*/
1;
執(zhí)行SQL語(yǔ)句
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(); # 查詢當(dāng)前日期和時(shí)間,使用分號(hào)終止語(yǔ)句
+---------------------+
| NOW() |
+---------------------+
| 2017-10-20 22:07:14 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW()\g # 查詢當(dāng)前日期和時(shí)間,使用\g終止語(yǔ)句
+---------------------+
| NOW() |
+---------------------+
| 2017-10-20 22:07:56 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(), USER(), VERSION()\g # 查詢時(shí)間,用戶,系統(tǒng)版本,并在一列展示
+---------------------+----------------+-------------------------+
| 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 #查詢時(shí)間,用戶,系統(tǒng)版本,并在一行展示
*************************** 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表示不執(zhí)行上述語(yǔ)句
mysql> SELECT NOW(); SELECT USER(); SELECT VERSION(); # 在一行輸入多條語(yǔ)句
+---------------------+
| 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)
關(guān)于命令大小寫
一般用大寫字母表示SQL關(guān)鍵字和函數(shù)名;
用小寫字母表示數(shù)據(jù)庫(kù),表和列的名字。
創(chuàng)建數(shù)據(jù)庫(kù)
mysql> CREATE DATABASE sampdb; # 創(chuàng)建數(shù)據(jù)庫(kù)
Query OK, 1 row affected (0.00 sec)
mysql> SELECT DATABASE(); # 查看當(dāng)前數(shù)據(jù)庫(kù),結(jié)果為NULL
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> USE sampdb; # 把sampdb設(shè)置為當(dāng)前默認(rèn)選擇的數(shù)據(jù)庫(kù)
Database changed
mysql> SELECT DATABASE(); # 查看當(dāng)前數(shù)據(jù)庫(kù),結(jié)果為NULL
+------------+
| DATABASE() |
+------------+
| sampdb |
+------------+
1 row in set (0.00 sec)
mysql> source create_president.sql # 如果已經(jīng)在終端cd到create_president.sql路徑下,那么則可運(yùn)行該語(yǔ)句
Query OK, 0 rows affected (0.15 sec)
Query OK, 0 rows affected (0.23 sec)
補(bǔ)充,文件create_president.sql的內(nèi)容如下:
# 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表的結(jié)構(gòu)
+------------+-------------+------+-----+---------+-------+
| 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的簡(jiǎn)寫為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)
查看更加詳細(xì)的信息
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)
查看與給定模式相匹配的列
如果需要對(duì)所查找的信息加以限定,
mysql> DESCRIBE president '%name'; # 查找名稱中包含name的項(xiàng)
+------------+-------------+------+-----+---------+-------+
| 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的項(xiàng),另外一種寫法
+------------+-------------+------+-----+---------+-------+
| 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)
插入數(shù)據(jù)
利用insert添加行
一次添加一行數(shù)據(jù):
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');
一次添加多行數(shù)據(jù):
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)
補(bǔ)充,文件source insert_president.sql內(nèi)容如下:
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);
總結(jié)
登錄
mysql -u root -p
本節(jié)SQL語(yǔ)句
mysql> SELECT NOW(); # 查詢當(dāng)前日期和時(shí)間,使用分號(hào)終止語(yǔ)句
mysql> SELECT NOW()\g # 查詢當(dāng)前日期和時(shí)間,使用\g終止語(yǔ)句
mysql> SELECT NOW(), USER(), VERSION()\g # 查詢時(shí)間,用戶,系統(tǒng)版本,并在一列展示
mysql> SELECT NOW(), USER(), VERSION()\G #查詢時(shí)間,用戶,系統(tǒng)版本,并在一行展示
mysql> SELECT NOW(); SELECT USER(); SELECT VERSION(); # 在一行輸入多條語(yǔ)句
mysql> CREATE DATABASE sampdb; # 創(chuàng)建數(shù)據(jù)庫(kù)
mysql> SELECT DATABASE(); # 查看當(dāng)前數(shù)據(jù)庫(kù),結(jié)果為NULL
mysql> USE sampdb; # 把sampdb設(shè)置為當(dāng)前默認(rèn)選擇的數(shù)據(jù)庫(kù)
mysql> SELECT DATABASE(); # 查看當(dāng)前數(shù)據(jù)庫(kù),結(jié)果為sampdb
mysql> source create_president.sql # 如果已經(jīng)在終端cd到create_president.sql路徑下,那么則可運(yùn)行該語(yǔ)句
mysql> DESCRIBE president; # 查看president表的結(jié)構(gòu)
mysql> DESC president; # DESCRIBE的簡(jiǎn)寫為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的項(xiàng)
mysql> SHOW FIELDS FROM president like '%name'; # 查找名稱中包含name的項(xiàng),另外一種寫法
mysql> INSERT INTO president VALUES ('Washington','George',NULL,'Wakefield','VA','1732-02-22','1799-12-14'); # 插入數(shù)據(jù)
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'); # 一次添加多行數(shù)據(jù)
mysql> source insert_president.sql # 利用文件添加新行
待補(bǔ)充……
總結(jié)
以上是生活随笔為你收集整理的mysql技术内幕sampdb_MySql技术内幕之MySQL入门(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 北理工在线作业计算机的主要特点是( ),
- 下一篇: [转载]地球物理经典书目——成像方向