mysql常用&實用語句

來源:互聯網
上載者:User

標籤:

Mysql是最流行的關係型資料庫管理系統,也是目前最常用的資料庫之一,掌握其常用的動作陳述式是必不可少的。

下面是自己總結的mysqp常用&實用的sql語句:

1、mysql -u root -p命令來串連到Mysql伺服器;

    mysqladmin -u root password "new_password"命令來建立root使用者的密碼。

2、查看當前有哪些DB:show databases;

    添加DB:create database mx;(mx資料庫名)

    刪除DB:drop database mx;

     使用DB:use mx;

3、建立資料表table

     creat table table_name(colum_name data_type,colum_name data_type,..colum_name data_type,);

    查看錶欄位:describe table_name;

4、增加列

    alter table 【table_name】add 【column_name】 【data_type】[not null][default];

    刪除列

    alter table 【table_name】drop 【column_name】;

5、修改列資訊

  alter table 【table_name】change 【old_column_name】 【new_column_name】【data_type】

    只改列名:data_type和原來一樣,old_column_name != new_column_name

  只改資料類型:old_column_name == new_column_name, data_type改變

  列名和資料類型都改了

6、修改表名

  alter table 【table_name】rename【new_table_name】;

7、查看錶資料

    select * from table_name;

  select col_name,col_name2,...from table_name;

8、插入資料

    insert into 【table_name】 value(值1,值2,...);

    insert into 【table_name】 (列1,列2...)value(值1,值2,...);

9、where語言

    select * from table_name where col_name 運算子 值;

    組合條件 and、or 

    where後面可以通過and與or運算子組合多個條件式篩選

  select * from table_name where col1 = xxx and col2 = xx or col > xx

10、null的判斷 - is /is not

    select * from table_name where col_name is null;

    select * from table_name where col_name is not null:

11、distinct(精確的)

    select distinct col_name from table_name;

12、order by排序

    按單一列名排序:

    select * from table_name [where 子句] order by col_name [asc/desc];

    按多列排序:

    select * from table_name [where 子句] order by  col1_name [asc/desc], col2_name [asc/desc]...;

    不加asc或者desc時,預設為asc

13、limit限制

  select * from table_name [where 子句] [order by子句] limit [offset,] rowCount;

    offset:查詢結果的起始位置,第一條記錄的其實是0

    rowCoun:從offset位置開始,擷取的記錄條數

    註:limit rowCount = limit 0,rowCount

14、insert into與select組合使用

    insert into 【表名1】 select 列1, 列2 from 【表名2】;

    insert into 【表名1】 (列1, 列2) select 列3, 列4 from 【表名2】;

15、updata文法

    修改單列

    updata 表名 set 列名 = xxx [where 字句];

  修改多列

    updata 表名 set 列名1 = xxx, 列名2 = xxx...[where 字句];

16、in文法

    select * from 表名 where 列名 in (value1,value2...);

    select * from 表名 where 列名 in (select 列名 from 表名);

17、between文法

    select * from 表名 where 列名 between 值1 and 值2;

    select * from 表名 where 列名 not between 值1 and 值2;

18、like文法

    select * from 表名 where 列名 [not] like pattern;

    pattern:匹配模式 , 比如 ‘abc‘  ‘%abc‘  ‘abc%‘  ‘%abc%‘

    ‘%‘ 是一個萬用字元,理解上可以把它當成任何字串

    例如:‘%abc‘   能匹配  ‘erttsabc‘

 

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.