1. 程式人生 > >mysql(三)-數據庫操作

mysql(三)-數據庫操作

mysql

數據庫存儲引擎

設置默認的存儲引擎:

vim /etc/my.conf
[mysqld]
default_storage_engine= InnoDB;

查看mysql支持的存儲引擎:

show engines;

查看當前默認的存儲引擎:

show variables like ‘%storage_engine%‘;

查看庫中所有表使用的存儲引擎

Show table status from db_name;

查看庫中指定表的存儲引擎

show table status like ‘tb_name‘;
show create table tb_name;

設置表的存儲引擎:

CREATE TABLE tb_name(... ) ENGINE=InnoDB;
ALTER TABLE tb_name ENGINE=InnoDB;

數據庫操作

命名規則:

必須以字母開頭
可包括數字和三個特殊字符(# _ $)
不要使用MySQL的保留字
同一database(Schema)下的對象不能同名

創建數據庫

create database if not exists newdb;       if not exists:條件判斷,如果不存在就創建數據庫

創建一個默認字符集為utf8的數據庫
create database db default character set utf8;
create database db default collate ‘utf8_general_ci‘;

查看一個數據庫創建時的默認字符集

show create database newdb;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| newdb    | CREATE DATABASE `newdb` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+

查看支持所有字符集

SHOW CHARACTER SET;

查看支持所有排序規則

SHOW COLLATION;

修改數據庫

語法:

ALTER database 數據庫名 修改的屬性 ...

修改指定數據庫默認字符集
alter database db default character set utf8;

刪除數據庫(刪除操作無法恢復)

drop database if exists newdb;               if exists:條件判斷,如果不存在就創建數據庫

數據表操作

查看表結構

desc 表名;

查看表狀態

show table status from db

創建表

全新創建表

CREATE TABLE [IF NOT EXISTS] ‘表名‘ (字段名1 類型 修飾符, 字段名2 類型 修飾符, ...)

基於現有表創建新表,主鍵會丟失

create table 新表名 select * from 舊表名;

基於現有表數據插入到新表,新表結構必須與舊表一致

insert into db select * from mysql.user;
所有類型:
?NULL 數據列可包含NULL值
?NOT NULL 數據列不允許包含NULL值
?DEFAULT 默認值
?PRIMARY KEY 主鍵
?UNIQUE KEY 唯一鍵
?CHARACTER SET name 指定一個字符集
?數值型
?AUTO_INCREMENT 自動遞增,適用於整數類型
?UNSIGNED 無符號

示例

create table students 
    -> (id int unsigned not null primary key,
    -> name varchar(20) not null,
    -> age tinyint unsigned);

desc students;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | int(10) unsigned    | NO   | PRI | NULL    |       |
| name  | varchar(20)         | NO   |     | NULL    |       |
| age   | tinyint(3) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+

取值範圍如果加了unsigned,則最大值翻倍,如tinyint unsigned的取值範圍為(0~255)

表別名

select user as 用戶,host as 主機 from mysql.user;
+--------+-----------+
| 用戶   | 主機      |
+--------+-----------+
| hunk3  | %         |
| root   | 127.0.0.1 |

創建一個純粹的索引

優點:提高查詢速度

缺點:占用額外空間,影響插入速度

索引實現在存儲引擎

查看表上的索引

SHOW INDEXES FROM [db_name.]tbl_name;

在沒有索引前,找到RAFAH記錄一共找了4321行

explain select * from city where NAME=‘RAFAH‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: city
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4321             查找了多少行
        Extra: Using where

創建索引

create index 索引名 on 表名(需要索引的字段)

create index index_name on city(name);
*************************** 3. row ***************************
        Table: city
   Non_unique: 1
     Key_name: index_name       這裏是索引名
 Seq_in_index: 1
  Column_name: Name
    Collation: A
  Cardinality: 184
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 

同時,查看表結構的時候,發現在Name中的key欄出現了MUL
desc city
    -> ;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   | MUL |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+

利用了索引之後

explain select * from city where NAME=‘RAFAH‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: city
         type: ref
possible_keys: index_name
          key: index_name
      key_len: 35
          ref: const
         rows: 1                查找了一行
        Extra: Using index condition

刪除索引

drop index 索引名 on 表名

drop index index_name on city;

mysql(三)-數據庫操作