修改提示符:html
PROMPT \u@\h \d >mysql
\D |
完整的日期 |
\d |
當前數據庫 |
\h |
服務器名稱 |
\u |
當前用戶 |
root 登陸建立用戶,賦予權限,鏈接方式兩種都可, 鏈接本地能夠不➕-hsql
$ mysql -uroot -proot數據庫
mysql>
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'myuser';服務器
mysql>
GRANT ALL ON test.* TO 'myuser'@'localhost'socket
$ mysql -umyuser -pmyuseride
$ mysql -u myuser -p ui
$ mysql -h localhost -u myuser -pthis
若是出現如下錯誤表示,MySQL server daemon (Unix) or service (Windows) 沒有在運行spa
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
退出鏈接
mysql> quit
mysql> \q
快捷鍵: Control+D.
這部分並不須要選擇任何數據庫。查詢當前時間,版本,用戶,當計算器使用
mysql> SELECT VERSION(), CURRENT_DATE; +------------+--------------+ | VERSION() | CURRENT_DATE | +------------+--------------+ | 5.6.40-log | 2018-07-19 | +------------+--------------+ 1 row in set (0.00 sec) mysql> SELECT VERSION(), CURRENT_DATE; +------------+--------------+ | VERSION() | CURRENT_DATE | +------------+--------------+ | 5.6.40-log | 2018-07-19 | +------------+--------------+ 1 row in set (0.00 sec) mysql> SELECT SIN(PI()/4), (4+1)*5; +--------------------+---------+ | SIN(PI()/4) | (4+1)*5 | +--------------------+---------+ | 0.7071067811865475 | 25 | +--------------------+---------+ 1 row in set (0.00 sec) mysql> SELECT VERSION(); SELECT NOW(); +------------+ | VERSION() | +------------+ | 5.6.40-log | +------------+ 1 row in set (0.00 sec) +---------------------+ | NOW() | +---------------------+ | 2018-07-19 15:41:16 | +---------------------+ 1 row in set (0.00 sec) musql> SELECT USER(),CURRENT_DATE; +------------------+--------------+ | USER() | CURRENT_DATE | +------------------+--------------+ | myuser@localhost | 2018-07-19 | +------------------+--------------+ 1 row in set (0.00 sec)
\c 切換回 mysql>
mysql> select -> user() -> \c mysql>
其餘提示符:
Prompt | Meaning |
---|---|
mysql> |
Ready for new query |
-> |
Waiting for next line of multiple-line query 多行SQL |
'> |
Waiting for next line, waiting for completion of a string that began with a single quote (' ) |
"> |
Waiting for next line, waiting for completion of a string that began with a double quote (" ) |
`> |
Waiting for next line, waiting for completion of an identifier that began with a backtick (` ) |
/*> |
Waiting for next line, waiting for completion of a comment that began with /* |
USE
, like
QUIT
, 不須要分號做爲結尾.只能寫在一行
myuser@localhost (none) >SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) myuser@localhost (none) > USE test Database changed myuser@localhost test >
3.3.1 Creating and Selecting a Database建立和查詢數據庫
root 建立數據庫,並賦予權限
root@localhost (none) >CREATE DATABASE menagerie; Query OK, 1 row affected (0.00 sec) root@localhost (none) >GRANT ALL ON menagerie.* TO 'myuser'@'localhost' -> ; Query OK, 0 rows affected (0.00 sec)
myuser 登陸並使用
myuser@localhost (none) >show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | menagerie | | test | +--------------------+ 3 rows in set (0.00 sec) myuser@localhost (none) >use menagerie Database changed
顯示錶
myuser@localhost (none) >show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | menagerie | | test | +--------------------+ 3 rows in set (0.00 sec)
創建表
myuser@localhost menagerie > CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 57 Current database: menagerie Query OK, 0 rows affected (0.07 sec)
varchar 字符串類型適合相似:name
, owner
, 和 species 這些列,由於他們的長度是變化的。
varchar 長度在1 到65535 之間。 可使用alter table 進行修改。
不少類型的值只有固定的選項,例如性別sex:'male'
and'female'能夠簡化爲
'm'
and 'f'
固定長處使用char類型。
用data 存儲時間格式
顯示錶結構
myuser@localhost menagerie >SHOW TABLES; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 58 Current database: menagerie +---------------------+ | Tables_in_menagerie | +---------------------+ | pet | +---------------------+ 1 row in set (0.01 sec) myuser@localhost menagerie >DESCRIBE pet; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.01 sec) myuser@localhost menagerie >
建立你的表以後,你必須填充它,但是喲使用load data和insert 語句。默認的日期格式 'YYYY-MM-DD'
After creating your table, you need to populate it. The LOAD DATA
and INSERT
statements are useful for this.
Suppose that your pet records can be described as shown here. (Observe that MySQL expects dates in 'YYYY-MM-DD'
format; this may be different from what you are used to.)