MySQL系列--2.經常使用的命令

1 、建立數據庫mysql

#語法:
CREATE DATABASE  dbName;
#建立數據庫rms
create database rms;

二、切換數據庫sql

#選擇數據庫
USE dbName;
#選擇數據庫rms;
use rms;

三、 查看全部的數據庫名稱數據庫

SHOW DATABASES;

四、建立表code

#語法:
#columnName:字段名稱
#columnType:字段類型
CREATE TABLE  tableName(columnName,columnTypei);
#建立表customers
CREATE TABLE `customers` (
  `customerNumber` int(11) NOT NULL,
  `customerName` varchar(50) NOT NULL,
  `contactLastName` varchar(50) NOT NULL,
  `contactFirstName` varchar(50) NOT NULL,
  `phone` varchar(50) NOT NULL
) ;

五、查看數據庫中全部的表排序

SHOW TABLES;

六、查看錶結構it

#語法:
DESC tableName;
#查看customes的結構
DESC customers;

mysql> DESC customers;
+------------------+-------------+------+-----+---------+-------+
| Field            | Type        | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| customerNumber   | int(11)     | NO   |     | NULL    |       |
| customerName     | varchar(50) | NO   |     | NULL    |       |
| contactLastName  | varchar(50) | NO   |     | NULL    |       |
| contactFirstName | varchar(50) | NO   |     | NULL    |       |
| phone            | varchar(50) | NO   |     | NULL    |       |
+------------------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

七、插入數據io

#語法:
INSERT INTO tableName(columnName1,columnName2,columnName N)values(value1,value2,valueN);
#columeName也能夠不寫
INSERT INTO tableName values(value1,value2,valueN);
INSERT INTO  customers(customerNumber,customerName,contactLastName,contactFirstName,phone)values(001,"Stephen Wang",'Stephen','Wang','15687965432');

INSERT INTO customers(customerNumber,customerName,contactLastName,contactFirstName,phone)values(003,"Lucy Liu",'Lucy','Liu','15687965432');

八、查看錶中的數據table

#語法:
SELECT  * FROM tableName;
mysql> select  * from  customers;                                                                 +----------------+--------------+-----------------+------------------+-------------+
| customerNumber | customerName | contactLastName | contactFirstName | phone       |
+----------------+--------------+-----------------+------------------+-------------+
|              2 | Vicent Wang  | Vicent          | Wang             | 15687965438 |
|              1 | Stephen Wang | Stephen         | Wang             | 15687965432 |
|              3 | Lucy Liu     | Lucy            | Liu              | 15687965432 |
+----------------+--------------+-----------------+------------------+-------------+
3 rows in set (0.00 sec)
#查看錶中的某些字段,語法:
SELECT columnName1,columnName2 from tableName;
mysql> select customerNumber,customerName from customers;
+----------------+--------------+
| customerNumber | customerName |
+----------------+--------------+
|              2 | Vicent Wang  |
|              1 | Stephen Wang |
|              3 | Lucy Liu     |
+----------------+--------------+
3 rows in set (0.00 sec)
#where條件
#查看客戶編號爲1的記錄
mysql> select customerNumber,customerName from customers  where customerNumber=1;
+----------------+--------------+
| customerNumber | customerName |
+----------------+--------------+
|              1 | Stephen Wang |
+----------------+--------------+
1 row in set (0.00 sec)

九、 更新數據ast

#語法:
UPDATE tableName  SET columeName=value where  conditions;
#將客戶編號爲1的客戶手機號修改成15997654325
mysql> update customers set phone='15997654325' where customerNumber=1;
Query OK, 1 row affected (0.62 sec)
Rows matched: 1  Changed: 1  Warnings: 0

十、模糊查詢date

#語法:
select  * from tableName  where columnName like ''condition [and /or] [columeName = value]; 
#查詢表裏last name爲Wang的客戶
mysql> select  *  from  customers where contactFirstName like '%wang';
+----------------+--------------+-----------------+------------------+-------------+
| customerNumber | customerName | contactLastName | contactFirstName | phone       |
+----------------+--------------+-----------------+------------------+-------------+
|              2 | Vicent Wang  | Vicent          | Wang             | 15687965438 |
|              1 | Stephen Wang | Stephen         | Wang             | 15997654325 |
+----------------+--------------+-----------------+------------------+-------------+
2 rows in set (0.08 sec)

十一、排序與分組

排序語法:
select  * from  tableName  order by columnName asc / desc;
#按照客戶編號遞增排序
mysql> select *  from customers order by customerNumber asc;
+----------------+--------------+-----------------+------------------+-------------+
| customerNumber | customerName | contactLastName | contactFirstName | phone       |
+----------------+--------------+-----------------+------------------+-------------+
|              1 | Stephen Wang | Stephen         | Wang             | 15997654325 |
|              2 | Vicent Wang  | Vicent          | Wang             | 15687965438 |
|              3 | Lucy Liu     | Lucy            | Liu              | 15687965432 |
+----------------+--------------+-----------------+------------------+-------------+
3 rows in set (0.00 sec)
#分組語法

#按照contactFirstName分組並統計客戶個數
mysql> select  contactFirstName,count(*)  from customers group by contactFirstName;
+------------------+----------+
| contactFirstName | count(*) |
+------------------+----------+
| Liu              |        1 |
| Wang             |        2 |
+------------------+----------+
2 rows in set (0.03 sec)

十二、修改字段名稱

#新增字段語法:
alter  table  tableName  add columnName; 
#修改字段語法:
alter  table  tableName  modify columnName; 
#刪除字段語法:
alter  table  tableName  drop  columnName; 
#customers添加一個狀態字段,類型爲char(20)
mysql> alter table  customers  add staus char(20);
Query OK, 0 rows affected (0.78 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select  * from  customers;
+----------------+--------------+-----------------+------------------+-------------+-------+
| customerNumber | customerName | contactLastName | contactFirstName | phone       | staus |
+----------------+--------------+-----------------+------------------+-------------+-------+
|              2 | Vicent Wang  | Vicent          | Wang             | 15687965438 | NULL  |
|              1 | Stephen Wang | Stephen         | Wang             | 15997654325 | NULL  |
|              3 | Lucy Liu     | Lucy            | Liu              | 15687965432 | NULL  |
+----------------+--------------+-----------------+------------------+-------------+-------+
3 rows in set (0.00 sec)

13.、刪除

#刪除表中的記錄,語法:
delete from tableName where conditions;
mysql> delete from  customers where customerNumber=1;
Query OK, 1 row affected (0.06 sec

#刪除表數據和結構,語法:
drop  table tableName;
mysql> drop table customers;
Query OK, 0 rows affected (0.16 sec)

#刪除數據庫,語法:
drop  database dbName;
mysql> drop database rms;
Query OK, 0 rows affected (0.13 sec)
相關文章
相關標籤/搜索