第10章 使用MySQL資料庫

來源:互聯網
上載者:User

標籤:

1.在資料庫中插入資料:INSERT語句;

        如://插入一整行:

               insert into customers values

                     (NULL,’…’,’…’,’…’), … ;

               //插入一行中指定的列內容:

               insert into customers (name,city) values

                    (‘…’,’…’);

               //等同於以下形式

              insert into cutomers

              set name=’…’,

                    city=’…’;

2.通過輸入cmd命令運行sql指令碼:

           >mysql –u root –p books < G:/Apache/htdocs/ch10/book_insert.sql

3.1.從資料庫中擷取資料:

           //擷取表中的指定列 

          select name,city

          from customers;

          //獲得表中所有的列和行

         select *

         from order_items;

         //獲得表中所有列中符合特定條件的行

         select *

         from orders

         where customerid=3 ;

         //可以用簡單的操作符、模式比對文法及AND和OR

         select *

         from orders

         where customerid=3 or customerid=4 ;

3.2.從多個表中擷取資料:

     ①簡單雙表關聯:

              select orders.orderid, orders.amount, orders.date

              from customers, orders

              where customers.name=’…’

              and customers.customerid=orders.customerid;

    ②尋找不匹配行:

           【左關聯:在兩個表之間指定的關聯條件下匹配資料行,如果右邊的表中沒有匹配行,結果中就會增加一行,該行右邊的列內容為NULL】(對應的右關聯同理)

            //ON文法

            select customers.customerid, customers.name, orders.orderid

           from customers left join orders

           on customers.customerid=orders.customerid;

           //USING文法,不需要指定串連屬性所來自的表

           select customers.customerid, customers.name

           from customers left join orders

           using (customerid)

          where orders.orderid is null;

   ③使用表的別名:Aliases (在一個查詢的開始建立表的別名,然後在整個查詢過程中使用)

          select c.name

          from customers as c, orders as o, order_items as oi, books as b

          where c.customerid=o.customerid

          and o.orderid=oi.orderid

          and oi.isbn=b.isbn

          and b.title like ‘%Java%’;

3.3.以特定順序擷取資料:

        如://按照名升序排列

               select name, address

               from customers

               order by name asc;    //ORDER BY子句預設為升序(即asc),desc為降序

4.分組、合計資料:

         合計函數:avg(列),count(項目),min(列),max(列),std(列),stddev(列),sun(列)

5.LIMIT:

         select name

         from customers

         limit 2, 3;     //從customers表中選擇name列,返回3行,從第2行開始

6.更新資料庫記錄:UPDATE語句

               如: update  customers

                       set …

                      where …

                      order by …

                      limit …

7.修改表:ALTER TABLE語句

         如://改變名稱允許的最大長度

                alter table customers

                modify name char(70) not null;

                //刪除一列

                alter table orders

                drop tax;

8.刪除資料庫中的記錄:

       ①DELETE語句:

                  如:  delete from customers

                           where customerid=5;

       ②刪除表:    如:drop table tablename;

       ③刪除整個資料庫:   如:drop database dbname;

第10章 使用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.