广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 数据库SQL语句---DDL语句
  • 672
分享到

MySQL 数据库SQL语句---DDL语句

2024-04-02 19:04:59 672人浏览 独家记忆
摘要

SQL语句---DDL语句==============================================================================概述:=========

SQL语句---DDL语句

==============================================================================

概述:


==============================================================================

MySQL服务端SQL语句 

   ---服务端命令:SQL语句,发往服务端运行,并取回结果;需要显式的语句结束符;

DDL:数据定义语言,

作用:

  • 主要用于数据库组件,例如数据库、表、索引、视图、触发器、事件调度器、存储过程、存储函数;

常用命令:

  • CREATE(创建), ALTER(修改), DROP(删除)(?后跟命令可获取帮助)

DML:数据操纵语言

作用:

  • CRUD(增删改查)操作,主要用于操作表中的数据;每一种操作之前都要先查询;

命令

  • INSERT,DELETE,UPDATE,SELECT

DCL:数据控制语言

作用:

  • 授权用户,登录主机地址权限及回收权限

命令

  • GRANT(授权), REVOKE(回收权限)


MySQL 数据库SQL语句---DDL语句

  SQL MODE:定义mysqld对约束等违反时的响应行为等设定;

★常用的MODE:

  • TRADITioNAL :         传统的模式,违反数据定义的统统都不被允许;

  • STRICT_TRANS_TABLES : 仅对事物型表严格限定;

  • STRICT_ALL_TABLES :   对所有的表都做严格限定;

修改方式:

  • Mysql> SET GLOBAL sql_mode='MODE';

  • mysql> SET @@global.sql_mode='MODE';

注意:

  • 默认为空模式,如果违反数据定义,会发出警报,会以允许的最大范围去修减数据

  • sql mode为必改参数,要想永久生效,要写入配置文件

演示:

 1.在sql mode模式为空的时候(默认),向表中插入数据,可以插入成功,但对违反数据定义的会对数据进行修减到允许的最大范围,如下:

MariaDB [(none)]> SELECT @@session.sql_mode;
+--------------------+
| @@session.sql_mode |
+--------------------+
|                    |
+--------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> CREATE DATABASE testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table tbl1(id tinyint unsigned,name CHAR(5));
Query OK, 0 rows affected (0.03 sec)

MariaDB [testdb]> insert into tbl1 (id) values (16),(256); # 默认最大为255
Query OK, 2 rows affected, 1 warning (0.00 sec) # 报错
Records: 2  Duplicates: 0  Warnings: 1

MariaDB [testdb]> select * from tbl1;
+------+------+
| id   | name |
+------+------+
|   16 | NULL |
|  255 | NULL |  # 可以发现我们插入的256没有成功,只到允许插入的最大范围
+------+------+
2 rows in set (0.00 sec)

MariaDB [testdb]> insert into tbl1 (name) values ('jerry'),('taotaoxiuxiu');
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 1

MariaDB [testdb]> show Warnings;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1265 | Data truncated for column 'name' at row 2 |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)

MariaDB [testdb]> select * from tbl1;
+------+-------+
| id   | name  |
+------+-------+
|   16 | NULL  |
|  255 | NULL  |
| NULL | jerry |
| NULL | taota |  # 我们定义的最大只能插入5个字符,多以多出来的将会被修减
+------+-------+
4 rows in set (0.00 sec)


  2.现在我们定义sql mode模式为TRADITIONAL(传统模式),即对数据进行严格的限定,对违反数据要求的统统不予许插入,如下:

MariaDB [testdb]> SET @@session.sql_mode='TRADITIONAL'; # 设定当前会话为传统模式;
Query OK, 0 rows affected (0.00 sec)

MariaDB [testdb]> SELECT @@session.sql_mode;
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| @@session.sql_mode                                                                                                                                   |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [testdb]> insert into tbl1 (name) values ('jerry'),('taotaoxiuxiu');
ERROR 1406 (22001): Data too long for column 'name' at row 2  # 再次插入报错,不允许插入




SQL语句之DDL语句

 1.获取帮助

  • mysql> help KEYWord

  • mysql> help contents

演示:

MariaDB [(none)]> help contents
You asked for help about help cateGory: "Contents"
For more infORMation, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

MariaDB [(none)]> help Data Types # 获取数据类型
You asked for help about help category: "Data Types"
For more information, type 'help <item>', where <item> is one of the following
topics:
   AUTO_INCREMENT
   BIGINT
   BINARY
   BIT
   BLOB
   BLOB DATA TYPE
   BOOLEAN
   CHAR
   CHAR BYTE
   DATE
   DATETIME
   DEC
   DECIMAL
   DOUBLE
   DOUBLE PRECISION
   ENUM
   FLOAT
   INT
   INTEGER
   LONGBLOB
   LONGTEXT
   MEDIUMBLOB
   MEDIUMINT
   MEDIUMTEXT
   SET DATA TYPE
   SMALLINT
   TEXT
   TIME
   TIMESTAMP
   TINYBLOB
   TINYINT
   TINYTEXT
   VARBINARY
   VARCHAR
   YEAR DATA TYPE

MariaDB [(none)]> help INT
Name: 'INT'
Description:
INT[(M)] [UNSIGNED] [ZEROFILL]

A normal-size integer. The signed range is -2147483648 to 2147483647.
The unsigned range is 0 to 4294967295.

URL: Http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

 



 2.数据库管理

创建数据库:

  • CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name  [DEFAULT] CHARACTER SET [=] charset_name 

  • character 字符集,SHOW CHARACTER SET 可查看所支持的字符集 

修改数据库

  • ALTER {DATABASE | SCHEMA} [db_name] CHARACTER SET [=] charset_name

删除数据库

  • DROP {DATABASE | SCHEMA} [IF EXISTS] db_name 

相关命令:

  • SHOW CHARACTER SET   //查看字符集;

  • SHOW COLLATION          //查看排序规则;

  • SHOW CREATE DATABASE db_name    //查看创建数据库时所使用的语句;

命令演示:

MariaDB [(none)]> show create database mydb; # 查看创建数据库mydb时的使用语句
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb`  |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> alter database mydb character set 'utf8'; # 修改字符集
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show create database mydb; # 查看库创建
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb`  |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)




3.表管理

  1)表创建

语法:

  • CREATE TABLE [IF NOT EXISTS] tbl_name  (create_definition,...) [table_options]

create_definition:由逗号分隔的列表

◆字段定义:

  • column_name column_defination 字段名称+字段定义相关信息

◆约束定义:

  • PRIMARY KEY(col1[,col2, ....])

  • UNIQUE KEY 

  • FOREIGN KEY 

  • CHECK(expr)

◆索引定义:

  • {INDEX|KEY}  普通索引创建

  • {FULLTEXT|SPATIAL} 全文索引,空间索引

注意:column_definition:

  • data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string']

table_option:

  • ENGINE [=] engine_name 存储引擎

查看数据库支持的存储引擎种类:

  • mysql> SHOW ENGINES;

查看指定表的存储引擎:

  • mysql> SHOW TABLE STATUS LIKE clause;

查看表结构定义:

  • DESC tbl_name;

查看表状态属性信息:

  • SHOW TABLE STATUS [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]

演示:

  1.表创建:

[root@Centos7 ~]# mysql -p134296
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 28
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
| ultrax             |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> USE mydb;
Database changed
MariaDB [mydb]> CREATE TABLE tbl1 (id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,name CHAR(30) NOT NULL,age TINYINT UNSIGNED,gender ENUM('F','M') DEFAULT 'M',UNIQUE KEY(name,gender),INDEX(name));
Query OK, 0 rows affected (0.04 sec)

MariaDB [mydb]> DESC tbl1;
+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| id     | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| name   | char(30)             | NO   | MUL | NULL    |                |
| age    | tinyint(3) unsigned  | YES  |     | NULL    |                |
| gender | enum('F','M')        | YES  |     | M       |                |
+--------+----------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

  2.查看存储引擎类型:

MariaDB [(none)]> show engines;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                    | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| CSV                | YES     | CSV storage engine                                                         | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                                      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)             | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                  | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                         | NO           | NO   | NO         |
| ARCHive            | YES     | Archive storage engine                                                     | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                                      | NO           | NO   | NO         |
| FEDERATED          | YES     | FederatedX pluggable storage engine                                        | YES          | NO   | YES        |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                     | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)

 3.查看表状态信息:

MariaDB [mydb]> show table status\G
*************************** 1. row ***************************
           Name: tbl1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 32768
      Data_free: 0
 Auto_increment: 1
    Create_time: 2016-10-16 17:54:32
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 

# 如果有多个表的话,可以使用where name 或者like 匹配相关的表        
MariaDB [(none)]> use mysql;
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
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.02 sec)

MariaDB [mysql]> show table status like 'proc%'\G # 匹配proc相关的表
*************************** 1. row ***************************
           Name: proc
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 292
Max_data_length: 281474976710655
   Index_length: 4096
      Data_free: 292
 Auto_increment: NULL
    Create_time: 2016-10-12 20:06:15
    Update_time: 2016-10-12 20:06:15
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: Stored Procedures
*************************** 2. row ***************************
           Name: procs_priv
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 239253730204057599
   Index_length: 4096
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-10-12 20:06:15
    Update_time: 2016-10-12 20:06:15
     Check_time: NULL
      Collation: utf8_bin
       Checksum: NULL
 Create_options: 
        Comment: Procedure privileges
2 rows in set (0.01 sec)

--------------------------------------------------------------------------------------------------------------------------------------

  2)表修改

语法:

  • ALTER  TABLE tbl_name [alter_specification [, alter_specification] ...]

alter_specification

◆表选项 

  • ENGINE=engine_name

     ...

◆表定义 

   字段

  • ADD :增

  • DRO:删

  • CHANGE :大改

  • MODIFY :局部范围小改动

键和索引

  • ADD {PRIMARY|UNIQUE|FOREIGN} key (col1, col2, ...)

  • ADD INDEX(col1, col2, ...)

  • DROP {PRIMARY|UNIQUE|FOREIGN} KEY key_name;

  • DROP INDEX index_name;

查看表上的索引信息:

  • SHOW INDEXES FROM tbl_name;

命令演示:

MariaDB [mydb]> use mydb
MariaDB [mydb]> show index from tbl1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            2 | gender      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| tbl1  |          1 | name_2   |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

MariaDB [mydb]> alter table tbl1 drop index name_2; # 删除索引name_2
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]>  show index from tbl1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            2 | gender      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

MariaDB [mydb]> desc tbl1;
+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| id     | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| name   | char(30)             | NO   | MUL | NULL    |                |
| age    | tinyint(3) unsigned  | YES  |     | NULL    |                |
| gender | enum('F','M')        | YES  |     | M       |                |
+--------+----------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

MariaDB [mydb]> alter table tbl1 add ClassID TINYINT UNSIGNED NOT NULL; # 新增加一个字段
Query OK, 0 rows affected (0.08 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]> desc tbl1;
+---------+----------------------+------+-----+---------+----------------+
| Field   | Type                 | Null | Key | Default | Extra          |
+---------+----------------------+------+-----+---------+----------------+
| id      | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| name    | char(30)             | NO   | MUL | NULL    |                |
| age     | tinyint(3) unsigned  | YES  |     | NULL    |                |
| gender  | enum('F','M')        | YES  |     | M       |                |
| ClassID | tinyint(3) unsigned  | NO   |     | NULL    |                |
+---------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

# 使用modify局部修改放到age的行后面
MariaDB [mydb]> alter table tbl1 modify  ClassID TINYINT UNSIGNED NOT NULL after age;
Query OK, 0 rows affected (0.11 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]> desc tbl1;
+---------+----------------------+------+-----+---------+----------------+
| Field   | Type                 | Null | Key | Default | Extra          |
+---------+----------------------+------+-----+---------+----------------+
| id      | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| name    | char(30)             | NO   | MUL | NULL    |                |
| age     | tinyint(3) unsigned  | YES  |     | NULL    |                |
| ClassID | tinyint(3) unsigned  | NO   |     | NULL    |                |
| gender  | enum('F','M')        | YES  |     | M       |                |
+---------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

-------------------------------------------------------------------------------

 3)表删除和查看表创建

表删除

  • DROP  TABLE [IF EXISTS] tbl_name [, tbl_name] ...

  • 可以一次删除多个表

查看表创建语句:

  • SHOW CREATE TABLE tbl_name 




 4.索引管理

引入索引的作用:

  • MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。

  • 实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。

索引类型

  • 聚集索引、非聚集索引:索引是否与数据存在一起;

  • 主键索引、辅助索引

  • 稠密索引、稀疏索引:是否索引了每一个数据项;

  • BTREE(B+)、HASH、R Tree、FULLTEXT

       BTREE:左前缀;

创建

语法:

  • CREATE  [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name ON tbl_name (index_col_name,...) 索引字段名称

  • index_col_name:

       col_name [(length)] [ASC | DESC]

  • {INDEX|KEY}  :普通索引创建

  • {FULLTEXT|SPATIAL} :全文索引,空间索引

删除:

  • DROP  INDEX index_name ON tbl_name

查看:

  • SHOW {INDEX | INDEXES | KEYS} {FROM | IN} tbl_name  [{FROM | IN} db_name] [WHERE expr]

使用ALTER 命令添加和删除索引

  • ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): 

        该语句添加一个主键,这意味着索引值必须是唯一的,且不能为NULL。

  • ALTER TABLE tbl_name ADD UNIQUE index_name (column_list):

        这条语句创建索引的值必须是唯一的(除了NULL外,NULL可能会出现多次)。

  • ALTER TABLE tbl_name ADD INDEX index_name (column_list):

        添加普通索引,索引值可出现多次。

  • ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list):

        该语句指定了索引为 FULLTEXT ,用于全文索引。

命令演示:

MariaDB [mydb]>  show index from tbl1; # 查看索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          0 | name     |            2 | gender      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.03 sec)

MariaDB [mydb]> drop index name on tbl1; # 删除索引
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]>  show index from tbl1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

MariaDB [mydb]> create index name_and_gender on tbl1(name(5),gender); # 创建索引
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]>  show index from tbl1; # 查看如下
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name        | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          0 | PRIMARY         |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| tbl1  |          1 | name_and_gender |            1 | name        | A         |           0 |        5 | NULL   |      | BTREE      |         |               |
| tbl1  |          1 | name_and_gender |            2 | gender      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

MariaDB [mydb]> show index from tbl1 where Key_name like 'name%'; # 查看指定的索引
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name        | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl1  |          1 | name_and_gender |            1 | name        | A         |           0 |        5 | NULL   |      | BTREE      |         |               |
| tbl1  |          1 | name_and_gender |            2 | gender      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.04 sec)




 5.VIEW 视图

★虚表:存储下来的SELECT语句;

创建:

  • CREATE  VIEW view_name [(column_list)] AS select_statement

修改:

  • ALTER  VIEW view_name [(column_list)] AS select_statement

删除:

  • DROP VIEW [IF EXISTS] view_name [, view_name] ...

演示:

MariaDB [testdb]> create table tbl2 (id INT UNSIGNED,name VARCHAR(50),age TINYINT UNSIGNED);
Query OK, 0 rows affected (0.04 sec)

MariaDB [testdb]> insert into tbl2 VALUES (1,'tom',21),(2,'tao',15),(3,'jing',22);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [testdb]> select * from tbl2;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | tom  |   21 |
|    2 | tao  |   15 |
|    3 | jing |   22 |
+------+------+------+
3 rows in set (0.00 sec)

MariaDB [testdb]> CREATE VIEW testview AS SELECT id,name FROM tbl2; # 创建VIEW
Query OK, 0 rows affected (0.02 sec)

MariaDB [testdb]> SHOW TABLES;  # 查看发现view也作为了一个表;
+------------------+
| Tables_in_testdb |
+------------------+
| tbl1             |
| tbl2             |
| testview         |
+------------------+
3 rows in set (0.00 sec)

MariaDB [testdb]> DESC testview;  # 但是数据只有id和name段,和原表tbl2不同
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | YES  |     | NULL    |       |
| name  | varchar(50)      | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

MariaDB [testdb]> select * from testview;
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | tao  |
|    3 | jing |
+------+------+
3 rows in set (0.00 sec)

MariaDB [testdb]> show table status\G   # 查看表的类型,可以看到第3张表为view类型的
*************************** 1. row ***************************
           Name: tbl1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-11-24 15:41:24
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
*************************** 2. row ***************************
           Name: tbl2
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 3
 Avg_row_length: 5461
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-11-24 16:43:04
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
*************************** 3. row ***************************
           Name: testview
         Engine: NULL
        Version: NULL
     Row_format: NULL
           Rows: NULL
 Avg_row_length: NULL
    Data_length: NULL
Max_data_length: NULL
   Index_length: NULL
      Data_free: NULL
 Auto_increment: NULL
    Create_time: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
3 rows in set (0.00 sec)

MariaDB [testdb]>  DROP VIEW testview;  # 删除view
Query OK, 0 rows affected (0.00 sec)

MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| tbl1             |
| tbl2             |
+------------------+
2 rows in set (0.00 sec)



 


您可能感兴趣的文档:

--结束END--

本文标题: MySQL 数据库SQL语句---DDL语句

本文链接: https://www.lsjlt.com/news/43711.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQL 数据库SQL语句---DDL语句
    SQL语句---DDL语句==============================================================================概述:=========...
    99+
    2022-10-18
  • 5 SQL语句之DDL
    DDL:Databse Definition Language...
    99+
    2022-10-18
  • 【MySQL】数据库SQL语句之DML
    目录 前言: 一.DML添加数据 1.1给指定字段添加数据 1.2给全部字段添加数据 1.3批量添加数据 二.DML修改数据 三.DML删除数据 四.结尾 前言:   时隔一周,啊苏今天来更新啦,简单说说这周在做些什么吧,上课、看书、...
    99+
    2023-08-31
    数据库 sql mysql
  • MySQL数据库 | SQL语句详解
    MySQL数据库基本操作——DDL DDL解释: 数据库的常用操作 表结构的常用操作 修改表结构 数据库的常用操作 查看所有的数据库show databases;创建数据库create database if not exists 表名;切...
    99+
    2023-08-23
    数据库 sql mysql
  • MySQL数据库常用SQL语句
    在写SQL时,经常灵活运用一些SQL语句编写的技巧,可以大大简化程序逻辑。减少程序与数据库的交互次数,有利于数据库高可用性,同时也能显得你的SQL很牛B,让同事们眼前一亮。实用的SQL1.插入或替换如果我们...
    99+
    2022-10-18
  • MySQL基础——DDL语句
    目录 MySQL SQL DDL——数据定义 数据库操作 连接 查询 使用 创建 删除 数据表操作 创建 查询 修改 删除 MySQL MySQL是一个关系型数据库管理系统,其数据是保存在不同的数据表中,而不是将所有数据放在一个大仓库内...
    99+
    2023-09-21
    mysql 数据库
  • MySQL基础教程14 —— SQL语法之数据定义语句DDL
    1. ALTER DATABASE语法 ALTER {DATABASE | SCHEMA} [db_name] alter_specification [, alter_specification] ... ...
    99+
    2022-05-17
    DDL MySQL SQL 数据库
  • MySQL数据定义语言DDL的基础语句
    MySQL DDL 语句 什么是DDL,DML。 DDL 是数据定义语言,就是对数据库,表层面的操作,如 CREATE,ALTER,DROP。DML 是数据操作语言,也就是对表中数据的增删改查,如 SELECT,UP...
    99+
    2022-05-26
    数据定义语言DDL MySQL DDL
  • SQL语句中的DDL类型的数据库定义语言操作
    目录SQL语句之DDL类型的数据库定义语言1.DDL类型的SQL语句基本概述2.DDL类型的SQL语句之数据库层面的操作2.1.创建一个数据库2.2.查看mysql中有哪些数据库2.3.进入某个数据库2.4.查看当前处于...
    99+
    2022-08-09
    SQL DDL数据库定义语言 SQL数据库定义语言
  • MYSQL中常见DDL语句
    DDL:对数据库以及数据库内部的对象进行创建、删除、修改等操作的语言,DDL语句更多的是由数据库管理员(DBA)使用,开发人员一般很少使用。 一、数据库: 查看数据库列表:show databases; 创建数据库:create data...
    99+
    2023-10-24
    数据库 mysql sql
  • MySQL ddl语句的使用
    前言 SQL的语言分类主要包含如下几种: DDL 数据定义语言 create、drop、alter 数据定义语言 create、...
    99+
    2022-05-23
    MySQL ddl语句 MySQL ddl
  • MySQL数据库中DDL语句的介绍和使用
    这篇文章的知识点包括:DDL语句的简介、DDL语句的操作以及DDL语句的使用注意事项,阅读完整文相信大家对MySQL数据库中DDL语句有了一定的认识。1.Online DDL简介在MySQL的早期版本中,D...
    99+
    2022-10-18
  • 【数据库】MySQL 高级(进阶) SQL 语句
    文章目录 前提条件一、常用查询1. SELECT(显示查询)2. DISTINCT(不重复查询)3. WHERE(有条件查询)4. AND/OR(且/或)5. IN (显示已知值的字段...
    99+
    2023-09-17
    数据库 mysql sql
  • SQL数据库语句大全
    目录基础创建数据库删除数据库备份sql server创建 备份数据的 device开始 备份创建新表根据已有的表创建新表:删除新表增加一个列添加主键删除主键创建索引删除索引创建视图删...
    99+
    2022-11-12
  • 【数据库】SQL 语句合集
    博客推行版本更新,成果积累制度,已经写过的博客还会再次更新,不断地琢磨,高质量高数量都是要追求的,工匠精神是学习必不可少的精神。因此,大家有何建议欢迎在评论区踊跃发言,你们的支持是我最大的动力,你们敢投,...
    99+
    2016-01-08
    【数据库】SQL 语句合集
  • 几条数据库Sql语句
    (1)同一张表中可能存在多辆车的皮重记录,想更新最早的一条记录,即更新其皮重。在sqlite3中,如下:update  CarNoTable set Tar...
    99+
    2022-10-18
  • 【MySQL】不允许你不会SQL语句之DDL
    目录 前言: 一.DDL数据库语句 1.1语句讲解 1.2总结 二.DDL表语句 2.1语句讲解 2.2总结 三.DDL字段语句 3.1语句讲解 3.2总结 四.MySQL数据类型 五.结尾 前言:   在从零到一入门MySQL一篇中...
    99+
    2023-09-01
    sql mysql 数据库
  • MySQL中DDL数据定义语句有哪些
    小编给大家分享一下MySQL中DDL数据定义语句有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、DDL数据定义语句库的管...
    99+
    2022-10-18
  • SQL 数据库T-SQL语句查询
             SQL 数据库T-SQL语句查询附加数据库的数据文件查询表中种类是水果的出厂日期在201-04-01之后的查询所有种类的总成本以倒序的方式...
    99+
    2022-10-18
  • MYSQL学习系列--DDL语句
    DDL语句: 对数据库内部的对象进行创建、删除、修改等操作的语言,DDL语句更多的是由数据库管理员(DBA)使用,开发人员一般很少使用登录mysql之后就可以使用sql语句对数据库进行各种操作啦! 实践操作...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作