1. 程式人生 > >MySQL基礎篇(庫/表的增刪改查)

MySQL基礎篇(庫/表的增刪改查)

上一篇我們已經在Linux系統裝好了MySQL,現在我們來熟悉一下MySQL及它的操作語句。

MySQL是一個關係型資料庫管理系統MySQL 是最流行的關係型資料庫管理系統之一.

設計模型:

  • E-R設計模型【E表示Entry,實體】、【R表示Relationship,關係】。
  • 一個實體轉換為資料庫中的一個表
  • 關係描述兩個實體之間對應規則,包括一對一、一對多、多對多
  • 關係轉換為資料表中的一個列*在關係型資料庫中一行就是一個物件

三正規化(Thread Normal Form):

  • 第一正規化(1NF):列不可拆分
  • 第二正規化(2NF):唯一標示
  • 第三正規化(3NF):引用主鍵

說明:後一個正規化都是在前一個正規化的基礎上建立的,在資料庫中一行的術語叫做記錄,一列叫做欄位,記錄和欄位在分別對應關係資料庫中的屬性和元組

登入MySQL

[email protected]:~$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.21-1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();  /*!檢視當前版本*/
+-----------+
| version() |
+-----------+
| 5.7.21-1  |
+-----------+
1 row in set (0.16 sec)

mysql> select now();      /*!顯示當前時間*/
+---------------------+
| now()               |
+---------------------+
| 2018-11-17 23:27:00 |
+---------------------+
1 row in set (0.10 sec)

mysql> 

 

資料庫操作:

查詢資料庫編碼資訊: Show variables like ‘character%’;

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

臨時更改客戶端和伺服器結果集的編碼:

Set charactersetclient=gbk;
Set charactersetresults=gbk;

檢視所有資料庫:show databases;

建立資料庫:create database 資料庫名 charset=utf8;

進入資料庫:use 資料庫名;

檢視當前所在資料庫:select database();

刪除資料庫:drop database 資料庫名;

例:

mysql> show databases;  /*!顯示所有資料庫*/
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydata             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> create database python charset=utf8; /*!建立名為:python的資料庫*/
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydata             |
| mysql              |
| performance_schema |
| python             | --------> 剛剛建立的python資料庫
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use python;  /*!使用python資料庫*/
Database changed
mysql> select database();  /*!檢視當前所在資料庫*/
+------------+
| database() |
+------------+
| python     |
+------------+
1 row in set (0.00 sec)

mysql> drop database python; /*!刪除python資料庫*/
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydata             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

表操作:

建立表:

建立表的基本格式    -------------    create table 表名(列及型別)

列及型別包括:

欄位名  資料型別(範圍) 是否允許空  主鍵  預設值   約束

檢視當前資料庫中的表:show tables;

查看錶結構:desc 表名;

更改表名稱:rename table 原表名 to 新表名;

  • alter 方法:alter table 表名 rename to 新表名

查看錶的建立語句:show table create 表名;

刪除表:drop table 表名;

修改表:

  • 增加欄位:alter table 表名 add  column  列名 型別     /*!此處的column可以不寫*/
  • 指定位置加入欄位:alter table 表名 add column 欄位名 欄位型別 after 某欄位
  • 刪除欄位:alter table 表名 drop 欄位名
  • 修改欄位名稱/型別:alter table 表名  change  舊欄位名 新欄位名 新欄位型別
  • 一次性清空表中所有資料(該方法也會是id從1開始):truncate table 表名

例:

+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(10) | NO   |     | NULL    |                |
| gender | bit(1)      | NO   |     | b'1'    |                |
+--------+-------------+------+-----+---------+----------------+

其中:auto_increment 表示自動增長。

例:

mysql> create table student(   /*!建立student表*/
    -> id int auto_increment primary key,
    -> name varchar(10) not null,
    -> birthday datetime(6) not null,
    -> gender bit(1) default 1 not null);
Query OK, 0 rows affected (0.63 sec)

mysql>  create table students(   /*!建立students表*/
    -> id int auto_increment primary key,
    ->  name varchar(10) not null,
    -> gender bit(1) default 1 not null);
Query OK, 0 rows affected (0.27 sec)

mysql> show tables;  /*!顯示所有表*/
+------------------+
| Tables_in_Python |
+------------------+
| student          |
| students         |
+------------------+
2 rows in set (0.00 sec)
mysql> desc student;   /*!查看錶結構*/
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(10) | NO   |     | NULL    |                |
| birthday | datetime(6) | NO   |     | NULL    |                |
| gender   | bit(1)      | NO   |     | b'1'    |                |
+----------+-------------+------+-----+---------+----------------+
mysql> desc students;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(10) | NO   |     | NULL    |                |
| gender | bit(1)      | NO   |     | b'1'    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> rename table student to student_info;  /*!將student改為student_info*/
Query OK, 0 rows affected (0.82 sec)

mysql> show tables;
+------------------+
| Tables_in_Python |
+------------------+
| student_info     |
| students         |
+------------------+
2 rows in set (0.00 sec)
mysql> show create table students;  /*!查看錶的建立語句*/
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                        |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `gender` bit(1) NOT NULL DEFAULT b'1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table students;   /*!刪除students表*/
Query OK, 0 rows affected (0.24 sec)
mysql> show tables;
+------------------+
| Tables_in_Python |
+------------------+
| student_info     |
+------------------+
1 row in set (0.00 sec)

mysql> alter table student_info add MathCsore int(10);  /*!增加MathScore列 型別int 長度10*/
Query OK, 0 rows affected (0.92 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student_info;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| name      | varchar(10) | NO   |     | NULL    |                |
| birthday  | datetime(6) | NO   |     | NULL    |                |
| gender    | bit(1)      | NO   |     | b'1'    |                |
| MathCsore | int(10)     | YES  |     | NULL    |                |  -------->增加的
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> 


備份與恢復

備份(匯出)

1、使用者切換為超級管理員

指定庫備份:

#mysqldump -uroot -p 庫名 > 備份檔案路徑

mysqldump -uroot -p  Python > ~/Desktop/backup.sql

多個庫備份:

#mysqldump -uroot -p --databases 庫名 > 備份檔案路徑

mysqldump -uroot -p databases Python mydata > ~/Desktop/dump.sql

恢復(匯入)

(1):系統命令方式:

mysql -uroot -p < 備份檔案路徑

(2):mysql命令列方法:

mysql> source 備份檔案路徑

具體資料操見下篇。。。