MySQL 資料庫簡單操作,mysql資料庫操作

來源:互聯網
上載者:User

MySQL 資料庫簡單操作,mysql資料庫操作

    對於想要從事或愛好mysql相關工作的童鞋們,有必要掌握在命令列下對mysql實現一些簡單的操作。本文從描述了如何登入到mysql資料庫伺服器,如何在mysql提示符下發布命令,建立資料庫,以及執行一些簡單的DML操作。

 

1、串連到與退出mysql

為了串連mysql資料庫伺服器,當調用mysql時,通常需要提供一個MySQL使用者名稱並且很可能需要一個密碼。如果伺服器運行在登入伺服器之外的其它機器上,還需要指定主機名稱。聯絡管理員以找出進行串連所使用的參數 (即,串連的主機、使用者名稱和使用的密碼)。知道正確的參數後,可以按照以下方式進行串連:shell> mysql -h host -u user -pmysql> select version(),current_date;+---------------------------------------+--------------+| version()                             | current_date |+---------------------------------------+--------------+| 5.6.17-enterprise-commercial-advanced | 2014-04-28   |+---------------------------------------+--------------+1 row in set (0.03 sec)--在允許匿名登入到本機伺服器的情下可以直接在shell提示符下直接輸入mysql即可實現登入mysql> #提示符告訴你mysql準備為你輸入命令。shell> mysql--輸入分號表示結束命令輸入並執行該命令--成功地串連後,可以在mysql>提示下輸入QUIT (或\q ,exit)隨時退出mysql> QUITBye--在Unix中,也可以按control-D鍵斷開伺服器。


2、發布命令

mysql執行命令可分為非互動與互動模式a) 非互動模式非互動模式,也叫批模式,也就是將想要啟動並執行命令放在一個檔案中,然後告訴mysql從檔案讀取它的輸入。通常用於返回資料量較大,以及批量管理,執行特殊指令碼啟動並執行情形。shell> mysql <query.sql[root@linux1 ~]# more query.sql show databases;use cnfoselect * from tb_tmp; [root@linux1 ~]# mysql -u root -pmysql <query.sqlWarning: Using a password on the command line interface can be insecure.Databaseinformation_schemacnfomysqlperformance_schematestname    sex     birthJack    F       2014-04-28John    M       2013-04-28--也可以使用下面的2種方式來執行批mysql > source /<dir>/filename mysql > \./<dir>/finename       --如下面的示範[root@linux1 ~]# mysql -u root -pmysqlmysql> source query.sql+--------------------+| Database           |+--------------------+| information_schema || cnfo               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed+------+------+------------+| name | sex  | birth      |+------+------+------------+| Jack | F    | 2014-04-28 || John | M    | 2013-04-28 |+------+------+------------+2 rows in set (0.00 sec)也可以在shell模式下直接執行SQL,如下面的方法:-e or --execution=option  shell>mysql -e "SQL cmd1;SQL cmd2;.."shell>mysql --execute="SQL cmd1;SQL cmd2;.."b) 互動模式互動模式就是直接在mysql提示符下發布命令並執行命令。如下操作,不區分大小寫,輸入斷行符號後會得到命令執行的結果,即為互動模式。mysql> SELECT VERSION(), CURRENT_DATE;mysql> select version(), current_date;mysql> SeLeCt vErSiOn(), current_DATE;--簡單計算mysql> select power(2,3),(5-1)*4;+------------+---------+| power(2,3) | (5-1)*4 |+------------+---------+|          8 |      16 |+------------+---------+1 row in set (0.00 sec)--分號分割多行mysql> select version();select current_date;+---------------------------------------+| version()                             |+---------------------------------------+| 5.6.17-enterprise-commercial-advanced |+---------------------------------------+1 row in set (0.01 sec)+--------------+| current_date |+--------------+| 2014-04-28   |+--------------+1 row in set (0.00 sec)--換行輸入命令--注,可以輸入空行mysql> select user(),    -> current_date;+----------------+--------------+| user()         | current_date |+----------------+--------------+| root@localhost | 2014-04-28   |+----------------+--------------+1 row in set (0.00 sec)--取消執行當前命令mysql> select current_date()\c


3、擷取mysql協助資訊
直接在提示符下輸入mysql --help會得到mysql命令所有參數的相關協助資訊,可以配合管道符more使用
shell> mysql --help

 

4、mysql常用提示符的含義
提示符    含義
mysql>    準備好接受新命令。
->        等待多行命令的下一行。
'>        等待下一行,等待以單引號(“'”)開始的字串的結束。
">        等待下一行,等待以雙引號(“"”)開始的字串的結束。
`>        等待下一行,等待以反斜點(‘`’)開始的識別符的結束。
/*>       等待下一行,等待以/*開始的注釋的結束。

 

5、日常操作

--建立資料庫mysql> create database cnfo;Query OK, 1 row affected (0.00 sec)--切換資料庫mysql> use cnfoDatabase changed--查看當前資料庫mysql> select database();+------------+| database() |+------------+| cnfo       |+------------+1 row in set (0.00 sec)--啟動mysql時串連到指定資料庫[root@linux1 ~]# mysql -u root -p cnfoEnter password: mysql> select database();+------------+| database() |+------------+| cnfo       |+------------+1 row in set (0.01 sec)--在當前庫建立表mysql> create table tb_tmp(name varchar(20),    -> sex char(1),birth date);Query OK, 0 rows affected (0.09 sec)--顯示當前庫所有的表mysql> show tables;+----------------+| Tables_in_cnfo |+----------------+| tb_tmp         |+----------------+1 row in set (0.00 sec)--查看錶的定義資訊mysql> desc tb_tmp    -> ;+-------+-------------+------+-----+---------+-------+| Field | Type        | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| name  | varchar(20) | YES  |     | NULL    |       || sex   | char(1)     | YES  |     | NULL    |       || birth | date        | YES  |     | NULL    |       |+-------+-------------+------+-----+---------+-------+3 rows in set (0.02 sec)-- Author : Leshami-- Blog   : http://blog.csdn.net/leshami--為表插入記錄mysql> insert into tb_tmp values('Jcack','F','20140428');Query OK, 1 row affected (0.08 sec)mysql> insert into tb_tmp values('John','M','20130428');Query OK, 1 row affected (0.02 sec)--查看錶上的記錄mysql> select * from tb_tmp;+-------+------+------------+| name  | sex  | birth      |+-------+------+------------+| Jcack | F    | 2014-04-28 || John  | M    | 2013-04-28 |+-------+------+------------+2 rows in set (0.00 sec)--更新表上的記錄mysql> update tb_tmp set name='Jack' where name='Jcack';Query OK, 1 row affected (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 0--過濾查詢mysql> select * from tb_tmp where sex='F';+------+------+------------+| name | sex  | birth      |+------+------+------------+| Jack | F    | 2014-04-28 |+------+------+------------+1 row in set (0.00 sec)

mysql中怎實現一個使用者只可以對一個資料庫進行操作的功可以?

安全性,登入名稱,雙擊,使用者映射,這是2008的操作,其他版本SQL可參照這個。
 
MySql資料庫管理軟體的操作方式有什?

針對於MySql資料庫的管理軟體有:
1、MySQL Workbench

2、Navicat for MySQL

這兩個無論哪個,管理和操作起來都很簡單。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.