码迷,mamicode.com
首页 > 数据库 > 详细

8.3 - mysql 表操作

时间:2018-03-12 18:48:16      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:ted   操作   rap   ORC   insecure   eve   statement   err   alt   

 

什么是存储引擎

mysql中建立的库===>文件夹

库中建立的表===>文件

存储引擎就是表的类型

 

现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型,处理表格用excel,处理图片用png等

数据库中的表也应该有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎。

存储引擎说白了就是如何存储数据、如何为存储的数据建立索引和如何更新、查询数据等技术的实现方
法。因为在关系数据库中数据的存储是以表的形式存储的,所以存储引擎也可以称为表类型(即存储和
操作此表的类型)

在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的。而MySql
数据库提供了多种存储引擎。用户可以根据不同的需求为数据表选择不同的存储引擎,用户也可以根据
自己的需要编写自己的存储引擎

mysql支持的存储引擎

mysql> show engines\G;
*************************** 1. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: CSV
     Support: YES
     Comment: CSV storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: Archive storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: NO
     Comment: Federated MySQL storage engine
Transactions: NULL
          XA: NULL
  Savepoints: NULL
9 rows in set (0.00 sec)

ERROR: 
No query specified
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

# 创建指定引擎的表
mysql> create table t2(id int) engine=innodb;
Query OK, 0 rows affected (0.03 sec)

# 查看创建的过程
mysql> show create table t2\G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified

# 其他类型的引擎方式
mysql> create table t3(id int) engine=memory;  # 存在内存中,只有表结构,没有数据
Query OK, 0 rows affected (0.00 sec)

mysql> create table t4(id int) engine=blackhole; # 存入的数据就没啦
Query OK, 0 rows affected (0.00 sec)

mysql> create table t5(id int) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t3 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t5 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;  # 存于内存,重启mysql就啦
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select * from t4;  # 数据没有啦,黑洞
Empty set (0.00 sec)

mysql> select * from t5;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

# 重启mysql 查看t3的数据
Usage: /etc/init.d/mysql start|stop|restart|reload|force-reload|status
leco@leco:/etc/mysql/mysql.conf.d$ /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.
leco@leco:/etc/mysql/mysql.conf.d$ mysql -uroot -pleco
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)

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> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
| t2            |
| t3            |
| t4            |
| t5            |
+---------------+
5 rows in set (0.00 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> select * from t3;  # 发现数据么有啦
Empty set (0.00 sec)

mysql 表的增删改查

技术分享图片

id,name,age,sex 是表字段,其他均是数据

创建表

语法

create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);

#注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的
4. []数据中是可有可无的
5. 每行有逗号,除了最后一条数据

详细操作步骤

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

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

mysql> use db2;
Database changed
mysql> create table t1(
    -> id int,
    -> name varchar(50),
    -> sex enum(male,female),
    -> age int(3)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum(male,female) | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

插入数据

mysql> insert into t1 values(1,cmz,male,18);  # 单条数据插入
Query OK, 1 row affected (0.01 sec)

mysql> insert into t1 values(2,leco,female,10),(3,loocha,male,8);  # 多条数据插入
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+------+--------+--------+------+
| id   | name   | sex    | age  |
+------+--------+--------+------+
|    1 | cmz    | male   |   18 |
|    2 | leco   | female |   10 |
|    3 | loocha | male   |    8 |
+------+--------+--------+------+
3 rows in set (0.00 sec)

 

修改表结构

语法:
1. 修改表名
      ALTER TABLE 表名 
                          RENAME 新表名;

2. 增加字段
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…],
                          ADD 字段名  数据类型 [完整性约束条件…];
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  FIRST;  # 字段放在开头
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名; # 字段放在 after字段,该字段后

3. 删除字段
      ALTER TABLE 表名 
                          DROP 字段名;

4. 修改字段
      ALTER TABLE 表名 
                          MODIFY  字段名 数据类型 [完整性约束条件…];
      ALTER TABLE 表名 
                          CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
      ALTER TABLE 表名 
                          CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

示例

 

复制表

1. 复制表结构 + 记录(数据)
2. 只复制表结构,不复制记录
1. 复制部分表结构
2. 复制全部表结构

复制表结构+记录 (key不会复制: 主键、外键和索引) # 复制表结构哦和数据 root@leco:
~# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | db2 | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> use db2; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | t1 | +---------------+ 1 row in set (0.00 sec) mysql> desc t1; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | sex | enum(male,female) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> select × from t1; ERROR 1054 (42S22): Unknown column × in field list mysql> select * from t1; +------+--------+--------+------+ | id | name | sex | age | +------+--------+--------+------+ | 1 | cmz | male | 18 | | 2 | leco | female | 10 | | 3 | loocha | male | 8 | +------+--------+--------+------+ 3 rows in set (0.00 sec) mysql> create table new_t1 select * from t1; Query OK, 3 rows affected (0.03 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | new_t1 | | t1 | +---------------+ 2 rows in set (0.00 sec) mysql> desc new_t1; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | sex | enum(male,female) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> select * from new_t1; +------+--------+--------+------+ | id | name | sex | age | +------+--------+--------+------+ | 1 | cmz | male | 18 | | 2 | leco | female | 10 | | 3 | loocha | male | 8 | +------+--------+--------+------+ 3 rows in set (0.00 sec) # 只复制表结构,不复制数据 mysql> create table t2 select * from t1 where 1=2; # 条件为假,查不到任何记录,此时就只会复制表结构,不复制表数据 Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | new_t1 | | t1 | | t2 | +---------------+ 3 rows in set (0.00 sec) mysql> desc t2; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | sex | enum(male,female) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> select * from t2; Empty set (0.00 sec) mysql> create table t3 like t1; Query OK, 0 rows affected (0.03 sec) mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | new_t1 | | t1 | | t2 | | t3 | +---------------+ 4 rows in set (0.00 sec) mysql> desc t3; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | sex | enum(male,female) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> select * from t3; Empty set (0.00 sec)

注意:

create table t2 select * from t1 where 1=2; 和 create table t3 like t1;都是创建表结构有神码区别?
前者可以选择性的复制,比如只复制其中部分字段的表结构,而后者是全部复制表结构字段。

 

删除表

 DROP TABLE 表名;
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| new_t1        |
| t1            |
| t2            |
| t3            |
+---------------+
4 rows in set (0.00 sec)

mysql> drop table t3;  # 指定表名
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| new_t1        |
| t1            |
| t2            |
+---------------+
3 rows in set (0.01 sec)

 

 

8.3 - mysql 表操作

标签:ted   操作   rap   ORC   insecure   eve   statement   err   alt   

原文地址:https://www.cnblogs.com/caimengzhi/p/8550408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!