【風馬一族_mysql】mysql基本指令

来源:http://www.cnblogs.com/sows/archive/2016/10/18/5971198.html
-Advertisement-
Play Games

船停在港灣是很安全的,但那不是造船的目的! 用戶 資料庫常用的指令: 數據表的常用指令 ...


船停在港灣是很安全的,但那不是造船的目的!

 

用戶

  1. 創建用戶
    1. mysql>grant 許可權(select,insert,update,delete) on  資料庫.數據表  to  用戶名@電腦的地址  identified by "用戶的密碼" 
      1. 許可權  【有四種許可權】
        1. select      查詢
        2. insert      插入
        3. update    修改
        4. delete  刪除 
      2. 資料庫.數據表 【有四種寫法】  
        1. *.*           表示所有資料庫與所有數據表          所有
        2. *.user          表示所有資料庫的user表               某個表的資料庫
        3. mysql.*        表示資料庫mysql的所有數據表       某個資料庫
        4. mysql.user  表示資料庫mysql的資料庫user表  某個資料庫的某個數據表
      3. 用戶名@電腦的地址
        1. 用戶名          要創建的用戶名稱,不能跟原先的用戶名相同
        2. 電腦的地址   mysql軟體所在的電腦連接
          1. 在自己電腦使用時,用 127.0.0.1
          2. 與伺服器的mysql交互時,用伺服器的IP  
    2. mysql> grant select,insert,update,delete on mysql.* to [email protected] identif ied by "ceosows";
      Query OK, 0 rows affected (0.01 sec)
    3. mysql> grant select,insert,update,delete on *.* to [email protected] identif ied by "ceosows";
      Query OK, 0 rows affected (0.01 sec)
  2. 查看用戶
    1. select * from 資料庫.數據表
    2. select * from mysql.user            

 

資料庫常用的指令:

  1. 創建資料庫
    1. mysql>create database 資料庫名 ;
  2. 顯示資料庫
    1. mysql>show databases;
    2. +--------------------+
      | Database                |
      +--------------------+
      | information_schema   |
      | mysql                       |
      | performance_schema |
      | test                         |
      | twwq                       |
      | xhkdb                      |
      +--------------------+
      6 rows in set (0.02 sec) 
  3. 刪除資料庫
    1. drop database 資料庫名;
    2. mysql> drop database twwq;
      Query OK, 0 rows affected (0.05 sec)
  4. 連接資料庫
    1. use 資料庫名;
    2. mysql>use twwq;
      Database changed
  5. 查看 當前連接的資料庫
    1. mysql>select database();
      +------------+
      | database()  |
      +------------+
      | twwq          |
      +------------+
      1 row in set (0.00 sec)

數據表的常用指令

  1. 創建表
    1. create table 表名(<欄位名1> <類型1> [,..<欄位名n> <類型n>]);        //警告:沒有給表填加欄位,會出錯
    2. mysql> create table registered(id int(8) not null primary key auto_increment,
          -> username char(20) not nul
          -> password char(20) not null,
          -> repassword char(20) not null);
      Query OK, 0 rows affected (0.19 sec)
  2. 查看表
    1. mysql>desc 表名 ; (方式一)      mysql> show columns from 表名; (方式二)
    2. mysql>desc registered; (方式一)      mysql> show columns from registered;(方式二 )
      +------------+----------+------+-----+---------+----------------+
      | Field        | Type       | Null   | Key  | Default   | Extra               |
      +------------+----------+------+-----+---------+----------------+
      | id               | int(8)       | NO   | PRI   | NULL      | auto_increment |
      | username    | char(20)  | NO    |        | NULL      |                        |
      | password    | char(20)  | NO    |        | NULL      |                        |
      | repassword | char(20)  | NO    |        | NULL      |                        |
      +------------+----------+------+-----+---------+----------------+
      4 rows in set (0.01 sec)
  3. 修改表
    1. rename table (原先的)表名  to (修改後的)表;
    2. mysql> rename table registered to sows;
      Query OK, 0 rows affected (0.06 sec)
    3. 查看效果
      1. mysql> desc sows;
        +------------+----------+------+-----+---------+----------------+
        | Field        | Type       | Null   | Key  | Default   | Extra              |
        +------------+----------+------+-----+---------+----------------+
        | id               | int(8)      | NO    | PRI   | NULL     | auto_increment |
        | name          | char(20)  | YES  |         | NULL     |                        |
        | password    | char(20)  | NO    |        | NULL     |                        |
        | repassword | char(20)  | NO    |        | NULL     |                        |
        +------------+----------+------+-----+---------+----------------+
        4 rows in set (0.01 sec)      
  4. 刪除表
    1. mysql>drop table 表名;            //警告:永久性刪除數據表,慎用
    2. mysql>drop table registered;
      Query OK, 0 rows affected (0.05 sec)  
  5. 增加    alter add命令用來增加表的欄位。
    1. 增添欄位
      1. alter table 數據表 add 欄位 參數 其他;
      2. mysql> alter table registered add sex char(2);
        Query OK, 0 rows affected (0.33 sec)
        Records: 0  Duplicates: 0  Warnings: 0
      3. 查看效果
        1. mysql> desc registered;
          +------------+----------+------+-----+---------+----------------+
          | Field           | Type        | Null   | Key | Default | Extra                 |
          +------------+----------+------+-----+---------+----------------+
          | id               | int(8)       | NO    | PRI | NULL    | auto_increment   |
          | username    | char(20)   | NO    |      | NULL    |                          |
          | password    | char(20)   | NO    |      | NULL    |                          |
          | repassword | char(20)   | NO    |      | NULL    |                          |
          | sex             | char(2)    | YES   |      | NULL    |                          |        //增加sex 欄位
          +------------+----------+------+-----+---------+----------------+
          5 rows in set (0.01 sec) 
        2. mysql> desc registered;   
          ERROR 1146 (42S02): Table 'twwq.registered' doesn't exist   //原因表名被修改,因此原先的表名,無法使用了
    2. 修改欄位
      1. alter table 數據表 change (要被修改的)欄位  (修改後的)欄位  參數;
      2. mysql> alter table registered change username name char(20);
        Query OK, 0 rows affected (0.33 sec)
        Records: 0  Duplicates: 0  Warnings: 0  
      3. 查看效果
        1. mysql> desc registered;
          +------------+----------+------+-----+---------+----------------+
          | Field           | Type       | Null    | Key  | Default  | Extra               |
          +------------+----------+------+-----+---------+----------------+
          | id               | int(8)      | NO     | PRI  | NULL     | auto_increment |
          | name          | char(20)  | YES   |        | NULL     |                        |        // username   修改成  name
          | password    | char(20)  | NO     |        | NULL     |                       |
          | repassword | char(20)  | NO     |         | NULL     |                      |
          | sex            | char(2)    | YES    |         | NULL     |                      |
          +------------+----------+------+-----+---------+----------------+
          5 rows in set (0.01 sec)   
    3. 刪除欄位
      1. alter table 數據表 drop 欄位;
      2. mysql> alter table registered drop sex;
        Query OK, 0 rows affected (0.31 sec)
        Records: 0  Duplicates: 0  Warnings: 0
      3. 查看效果
        1. mysql> desc registered;
          +------------+----------+------+-----+---------+----------------+
          | Field           | Type       | Null    | Key  | Default  | Extra               |
          +------------+----------+------+-----+---------+----------------+
          | id               | int(8)      | NO    | PRI   | NULL     | auto_increment |
          | name          | char(20)  | YES  |        | NULL      |                        |
          | password    | char(20)  | NO    |        | NULL     |                        |
          | repassword | char(20)  | NO    |        | NULL      |                       |
          +------------+----------+------+-----+---------+----------------+   //sex 欄位 已經被刪除
          4 rows in set (0.01 sec)             
    4. 加索引  
      1. alter table 數據表 add index 索引名(【已經存在於表的】欄位名);
      2. mysql> alter table registered add index sows_name(username);
        Query OK, 0 rows affected (0.17 sec)
        Records: 0  Duplicates: 0  Warnings: 0 
      3. 查看效果
        1. mysql> desc registered;
          +------------+----------+------+-----+---------+----------------+
          | Field           | Type       | Null    | Key | Default | Extra                 |
          +------------+----------+------+-----+---------+----------------+
          | id               | int(8)      | NO    | PRI   | NULL    | auto_increment  |
          | username    | char(20)  | NO    | MUL | NULL     |                        |   //Key 欄位出現 MUL
          | password    | char(20)  | NO    |        | NULL     |                        |
          | repassword | char(20)  | NO    |        | NULL     |                        |
          | sex            | char(2)    | YES   |        | NULL     |                        |
          +------------+----------+------+-----+---------+----------------+
          5 rows in set (0.01 sec)  
    5. 刪除索引
      1. alter table 數據表 drop index 索引名;
      2. mysql> alter table registered drop index sows_name;
        Query OK, 0 rows affected (0.15 sec)
        Records: 0  Duplicates: 0  Warnings: 0
      3. 查看效果
        1. mysql> desc registered;
          +------------+----------+------+-----+---------+----------------+
          | Field           | Type       | Null    | Key | Default   | Extra               |
          +------------+----------+------+-----+---------+----------------+
          | id               | int(8)      | NO    | PRI   | NULL     | auto_increment |
          | username    | char(20)  | NO    |        | NULL     |                        |  //key 欄位的內容消失
          | password    | char(20)  | NO    |        | NULL     |                        |
          | repassword | char(20)  | NO    |        | NULL     |                        |
          | sex            | char(2)    | YES   |        | NULL     |                        |
          +------------+----------+------+-----+---------+----------------+
          5 rows in set (0.01 sec)                
  6. 插入數據
    1. mysql>insert into 數據表(欄位  //如果已經實現步驟1,則不用再寫欄位) values (根據欄位的數量,變數類型、變數長度要求、是否允許為空,來進行填寫內容)
    2. mysql> insert into registered values (1,'sows','asqw1234','asqw1234'),(2,'ceo','
      aswe1322','aswe1322'),(3,'jk','qazxsw12','qazxsw12');
      Query OK, 3 rows affected (0.01 sec)
      Records: 3  Duplicates: 0  Warnings: 0      // 成功插入 3條數據
  7. 查看表中的數據    //準確性的查詢所需數據
    1. mysql>select * from 數據表;                         //獲取表中所有數據
      1. mysql>select * from  registered;
        +----+----------+----------+--------------+
        | id    | username | password | repassword |
        +----+----------+----------+--------------+
        |  1   | sows        | asqw1234 | asqw1234   |
        |  2   | ceo          | aswe1322  | aswe1322   |
        |  3   | jk            | qazxsw12  | qazxsw12   |
        +----+----------+----------+--------------+
        3 rows in set (0.00 sec)      //獲取到3條數據


    2. mysql>select * from 數據表 order by id limit 從哪個位置開始,一共幾條;
      1. mysql> select * from registered order by id limit 1,2;
        +----+----------+----------+------------+
        | id | username | password | repassword  |
        +----+----------+----------+------------+
        |  2 | ceo      | aswe1322 | aswe1322       |
        |  3 | jk       | qazxsw12 | qazxsw12        |
        +----+----------+----------+------------+
        2 rows in set (0.00 sec)     //獲取兩條數據
  8. 刪除表中的數據
    1. mysql>delect from 數據表  where 條件限制 ;  
    2. mysql> delete from registered where id=1;
      Query OK, 1 row affected (0.01 sec)  //刪除成功
    3. 使用5,查看刪除的效果
      1. mysql> select * from registered;
        +----+----------+----------+------------+
        | id    | username | password | repassword |
        +----+----------+----------+------------+
        |  2    | ceo         | aswe1322 | aswe1322   |
        |  3    | jk           | qazxsw12 | qazxsw12   |
        +----+----------+----------+------------+
        2 rows in set (0.00 sec)   //id為1的數據被成功刪除了  
  9. 修改表中的數據
    1. mysql>update 數據表 set (要修改數據的)欄位=修改後的數據   where  條件限制
      1. mysql> update registered set username='sowsceo' where id=2;
        Query OK, 1 row affected (0.01 sec)                          //修改成功
        Rows matched: 1  Changed: 1  Warnings: 0     //修改的數量
      2. 查看修改後的效果
        1. mysql> select * from registered;
          +----+----------+----------+------------+
          | id | username | password | repassword |
          +----+----------+----------+------------+
          |  2 | sowsceo  | aswe1322 | aswe1322   |      //username 從ceo 變成 sowsceo
          |  3 | jk       | qazxsw12 | qazxsw12   |
          +----+----------+----------+------------+
          2 rows in set (0.00 sec)   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、關於用戶 Oracle安裝會自動的生產sys用戶和system用戶: 1. sys用戶是超級用戶,具有最高許可權,具有sysdba角色,有create database的許可權,該用戶的預設密碼是change_on_install 。 2. system用戶是管理操作員,許可權也很大,具有sysope ...
  • 有個經典的題目:1-100之間的數字(不重覆)存放在表裡,共95行一列,但是裡面缺了5個數字,怎麼用SQL最快找出那五個數字。 我們先來看看Oracle資料庫如何實現,如下所示,我們先準備測試環境和數據。 SQL> create table t( id number(10)); Table crea... ...
  • 業務描述: 統計從kafka spout中讀取的數據條數,以及寫入redis的數據的條數,寫入hdfs的數據條數,寫入kafaka的數據條數。並且每過5秒將數據按照json文件的形式寫入日誌。其中保存為json數據的格式為:時間戳 + 進程名稱 + 讀kafka數據條數 + 寫入redis數據條數 ...
  • 1.《Microsoft SQL Server企業級平臺管理實踐》徐海蔚2.《SQL Server 2005 Performance Tuning性能調校》胡百敬3.《Microsoft SQL Server 2005技術內幕:存儲引擎》在國外,不可多得的好書如下4.《Professional SQ ...
  • 1.註冊Oracle賬戶: 註冊地址:https://login.oracle.com/mysso/signon.jsp 註意:註冊的時候儘量使用外國的郵箱,因為使用國內的郵箱可能收不到Oracle發送的信息! 當然你也可以從別的地方下載,只要能用就可以了。 2.下載Oracle Database ...
  • 本文來自:http://www.cnblogs.com/yangxia-test/p/3922775.html 一.下載 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下兩網址來源 ...
  • 安裝解壓版MySQL以後,不能啟動,日誌裡面出現了這個錯誤: 這是因為mysql服務啟動時候找不到內置資料庫“mysql”,找不到那張表,將之前的目錄裡面的“mysql”資料庫拷貝到新的資料庫文件存放目錄,即可解決這個問題。 ...
  • 1. 官網下載 wget http://apache.fayea.com/hadoop/common/hadoop-3.0.0-alpha1/hadoop-3.0.0-alpha1.tar.gz 2. 解壓 tar -zxvf hadoop-3.0.0-alpha1.tar.gz ln -s had ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...