MySQL資料庫的增刪改查

來源:互聯網
上載者:User

標籤:切換資料庫   命令   選擇   ora   ==   建立   刪除   ODB   star   

*在終端中開啟資料庫: 方式1.在管理員身份運行cmd終端中開啟資料庫: net start mysql57(服務名稱) ==>串連資料庫: mysql -u 使用者名稱 -p ==> 輸入密碼
                                    方式2.在mysql-client裡面開啟資料庫: 直接輸入密碼就可以進入了.
*退出登入: exit
*遠端連線資料庫: mysql -h ip地址 -u 對方使用者名稱 -p ==> 輸入對方密碼
*資料庫類型: 關係型資料庫: MySQL, Oracle等; 非關係型資料庫(NoSQL): MongoDB, Redis等.
*資料庫操作:show databases; 查看所有資料庫 # 查看資料庫,detabase要加複數s
                     create database demo charset=utf8; 建立資料庫demo # 資料庫命令末尾要加分號;
                     drop database demo; 刪除資料庫demo
                     use demo 切換資料庫 # 要操作資料庫得先選擇使用一個資料庫
                     select database; 查看當前使用的資料庫
*表操作: show tables; 查看所有的表 # 查看錶也要加s
              create table demo(列1,列2,...); 建立表demo 例: create table demo(id int auto_increment primary_key, name varchar(20))
              drop table demo; 刪除表demo
              desc demo; 查看錶結構
              show create table demo; 查看建立表demo所使用的語句
              rename table demo to newdemo; 將表demo的表名修改為newdemo
*資料操作:增: insert into 表名 values(...); 全列插入
                       insert into 表名 values(...),(...),...; 全列插入多條資料
                       insert into 表名(列1,列2,...) values(值1,值2,...) 預設插入
               刪除: delete from 表名 where 條件; 刪除表內資料(警告:後面不加where條件會將表內資料全部刪除) # 例:delete from student where id=4;
                   改: update 表名 set 列1=值1,列2=值2,...where 條件; 修改表內資料(警告:後面不加條件會將全部列都修改) # 例:update student set name=tom,age=18 where id=7;
                   查: select * from 表名; 查看某一個表裡面的所有資料(這裡的*代表所有的列, 如果只想查詢部分列可用逗號分隔, 例select name,age from 表名;)
條件查詢:  a.文法: select * from 表名 where 條件;
                 b.比較子(=, >, <, >=, <=, !=): 例: select * from student where id>=3;
                 c.邏輯運算子(and, or, not): 例: select * from student where id>=3 and gender=0; 查詢id>=3且性別為女的學生
                 d.模糊查詢: like: %表示任意多個任一字元、 _表示一個任一字元
                    例: select * from student where name like "周%"; => 得到結果如周杰倫, 周星馳...
                          select * from student where name like "周_"; => 得到結果如周迅...
                e.範圍查詢: 例: select * from student where id in (2,7,12); # 查詢id為2,7,12的學生
                                         select * from student where id between 5 and 10; # 查詢id在5-10之間的學生
                f.空查詢: 例: select * from student where address is null; # 查詢住址為空白的學生(非空是not null)
                g.優先順序: 小括弧 > not > 比較子 > 邏輯運算子 ; and > or =>可用()來改變優先順序
                h.排序: select * from 表名 order by 列1 asc/desc, 列2 asc/desc, ... ; # asc升序 desc降序

MySQL資料庫的增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.