Mysql 常用命令

來源:互聯網
上載者:User

標籤:hone   where   cti   delete   位移量   limit   size   phone   0.11   

一、Mysql 建立資料庫 

mysql> create database study charset utf8;Query OK, 1 row affected (0.01 sec)

 

二、建立資料表

  文法:

CREATE TABLE table_name (column_name column_type);

  建立一個student表

mysql> create table student(    -> stu_id int NOT NULL AUTO_INCREMENT,                   -> name char(32) NOT NULL,    -> age int NOT NULL,    -> register_date DATE NOT NULL,    -> primary key (stu_id)    -> );Query OK, 0 rows affected (0.02 sec)mysql> desc student;+---------------+----------+------+-----+---------+----------------+| Field         | Type     | Null | Key | Default | Extra          |+---------------+----------+------+-----+---------+----------------+| stu_id        | int(11)  | NO   | PRI | NULL    | auto_increment || name          | char(32) | NO   |     | NULL    |                || age           | int(11)  | NO   |     | NULL    |                || register_date | date     | NO   |     | NULL    |                |+---------------+----------+------+-----+---------+----------------+4 rows in set (0.01 sec)
  • 如果你不想欄位為 NULL 可以設定欄位的屬性為 NOT NULL, 在操作資料庫時如果輸入該欄位的資料為NULL ,就會報錯。
  • AUTO_INCREMENT定義列為自增的屬性,一般用於主鍵,數值會自動加1。
  • PRIMARY KEY關鍵字用於定義列為主鍵。 您可以使用多列來定義主鍵,列間以逗號分隔。

 

三、資料操作

  3.1 插入資料

  文法:  

INSERT INTO table_name ( field1, field2,...fieldN )                       VALUES                       ( value1, value2,...valueN );

  插入資料:

mysql> insert into student (name,age,register_date) values (‘bigberg‘,11,‘2018-01-01‘);Query OK, 1 row affected (0.01 sec)mysql> select * from student;+--------+---------+-----+---------------+| stu_id | name    | age | register_date |+--------+---------+-----+---------------+|      1 | bigberg |  11 | 2018-01-01    |+--------+---------+-----+---------------+1 row in set (0.00 sec)

  3.2 查詢資料

  文法:

SELECT column_name,column_nameFROM table_name[WHERE Clause][OFFSET M ][LIMIT N]
  • 查詢語句中你可以使用一個或者多個表,表之間使用逗號(,)分割,並使用WHERE語句來設定查詢條件。
  • SELECT 命令可以讀取一條或者多條記錄。
  • 你可以使用星號(*)來代替其他欄位,SELECT語句會返回表的所有欄位資料
  • 你可以使用 WHERE 語句來包含任何條件。
  • 你可以通過OFFSET指定SELECT語句開始查詢的資料位移量。預設情況下位移量為0, 如果使用不能單獨使用。
  • 你可以使用 LIMIT 屬性來設定返回的記錄數。
select * from student;select * from student where stu_id > 2;select * from student where register_date like ‘2018-02-%‘;
mysql> select * from student limit 3, 1;+--------+--------+-----+---------------+| stu_id | name   | age | register_date |+--------+--------+-----+---------------+|      4 | wangwu |  23 | 2018-02-14    |+--------+--------+-----+---------------+1 row in set (0.00 sec)# limit後面是從第3條開始讀,讀取1條資訊。
mysql> select * from student limit 3 offset 2;+--------+--------+-----+---------------+| stu_id | name   | age | register_date |+--------+--------+-----+---------------+|      3 | lisi   |  31 | 2018-02-11    ||      4 | wangwu |  23 | 2018-02-14    |+--------+--------+-----+---------------+2 rows in set (0.00 sec)# limit後面跟的是3條資料,offset後面是從第3條開始讀取

  3.3 where 字句

   文法:

SELECT field1, field2,...fieldN FROM table_name1, table_name2...[WHERE condition1 [AND [OR]] condition2.....  
操作符 描述 執行個體
= 等號,檢測兩個值是否相等,如果相等返回true (A = B) 返回false。
<>, != 不等於,檢測兩個值是否相等,如果不相等返回true (A != B) 返回 true。
> 大於符號,檢測左邊的值是否大於右邊的值, 如果左邊的值大於右邊的值返回true (A > B) 返回false。
< 小於符號,檢測左邊的值是否小於右邊的值, 如果左邊的值小於右邊的值返回true (A < B) 返回 true。
>= 大於等於符號,檢測左邊的值是否大於或等於右邊的值, 如果左邊的值大於或等於右邊的值返回true (A >= B) 返回false。
<= 小於等號,檢測左邊的值是否小于于或等於右邊的值, 如果左邊的值小於或等於右邊的值返回true (A <= B) 返回 true。

  使用主鍵來作為 WHERE 子句的條件查詢是非常快速的。

select * from student where register_date > ‘2016-03-04‘;

  3.4 UPDATE 查詢

  文法:

UPDATE table_name SET field1=new-value1, field2=new-value2[WHERE Clause]

  更新資料:

mysql> update student set age=22 where stu_id = 1;Query OK, 1 row affected (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 0

  3.5 DELETE 語句

  文法:

DELETE FROM table_name [WHERE Clause]

  刪除語句:

mysql> delete from student where stu_id = 4;Query OK, 1 row affected (0.00 sec)

  3.6 order by排序

  文法:

SELECT field1, field2,...fieldN table_name1, table_name2...ORDER BY field1, [field2...] [ASC [DESC]]

  排序語句:

# 使用 ASC 或 DESC 關鍵字來設定查詢結果是按升序或降序排列。 預設情況下,它是按升序排列。mysql> select name, age, register_date from student order by age DESC;+---------+-----+---------------+| name    | age | register_date |+---------+-----+---------------+| lisi    |  31 | 2018-02-11    || bigberg |  22 | 2018-01-01    || zhansan |  21 | 2018-01-11    |+---------+-----+---------------+3 rows in set (0.00 sec)
mysql> select name, age, register_date from student order by age;+---------+-----+---------------+| name    | age | register_date |+---------+-----+---------------+| zhansan |  21 | 2018-01-11    || bigberg |  22 | 2018-01-10    || wangwu  |  22 | 2018-01-02    || lisi    |  31 | 2018-02-11    |+---------+-----+---------------+4 rows in set (0.00 sec)mysql> select name, age, register_date from student order by age, register_date;+---------+-----+---------------+| name    | age | register_date |+---------+-----+---------------+| zhansan |  21 | 2018-01-11    || wangwu  |  22 | 2018-01-02    || bigberg |  22 | 2018-01-10    || lisi    |  31 | 2018-02-11    |+---------+-----+---------------+4 rows in set (0.00 sec)

  3.7 group by 分組

  文法:

SELECT column_name, function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

  group by 語句

mysql> select age,count(*) as number from student group by age;+-----+--------+| age | number |+-----+--------+|  21 |      1 ||  22 |      2 ||  31 |      1 |+-----+--------+3 rows in set (0.00 sec)

  3.8 ALTER命令

  文法:

# 我們需要修改資料表名或者修改資料表欄位時,就需要使用到MySQL ALTER命令alter table student drop register_date; #從student表刪除register_date   欄位alter table student add phone int(11) not null; #添加phone欄位

  增加和刪除欄位

# 增加一個性別欄位mysql> alter table student add gender enum(‘M‘,‘F‘) NOT NULL;Query OK, 0 rows affected (0.06 sec)Records: 0  Duplicates: 0  Warnings: 0# 刪除age欄位mysql> alter table student drop age;Query OK, 0 rows affected (0.04 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> select * from student;+--------+---------+---------------+--------+| stu_id | name    | register_date | gender |+--------+---------+---------------+--------+|      1 | bigberg | 2018-01-10    | M      ||      2 | zhansan | 2018-01-11    | M      ||      3 | lisi    | 2018-02-11    | M      ||      5 | wangwu  | 2018-01-02    | M      ||      6 | Lily    | 2018-02-23    | F      |+--------+---------+---------------+--------+5 rows in set (0.00 sec)

  修改欄位類型

mysql> DESC student;+---------------+---------------+------+-----+---------+----------------+| Field         | Type          | Null | Key | Default | Extra          |+---------------+---------------+------+-----+---------+----------------+| stu_id        | int(11)       | NO   | PRI | NULL    | auto_increment || name          | char(32)      | NO   |     | NULL    |                || register_date | date          | NO   |     | NULL    |                || gender        | enum(‘M‘,‘F‘) | NO   |     | NULL    |                |+---------------+---------------+------+-----+---------+----------------+4 rows in set (0.00 sec)mysql> alter table student modify name varchar(50);Query OK, 5 rows affected (0.11 sec)Records: 5  Duplicates: 0  Warnings: 0mysql> desc student;+---------------+---------------+------+-----+---------+----------------+| Field         | Type          | Null | Key | Default | Extra          |+---------------+---------------+------+-----+---------+----------------+| stu_id        | int(11)       | NO   | PRI | NULL    | auto_increment || name          | varchar(50)   | YES  |     | NULL    |                || register_date | date          | NO   |     | NULL    |                || gender        | enum(‘M‘,‘F‘) | NO   |     | NULL    |                |+---------------+---------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

  修改表名

mysql> alter table student rename to student_table;Query OK, 0 rows affected (0.01 sec)mysql> show tables;+-----------------+| Tables_in_study |+-----------------+| student_table   |+-----------------+1 row in set (0.00 sec)

  

 

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.