zoukankan      html  css  js  c++  java
  • Mysql简单总结

    基于Mac OS X系统

    MySQL的安装和配置

    首先进入 MySQL 官网,选择免费的Community版:MySQL Community Server。MySQL 官网提供了tar.gz和dmg两种格式的安装包,对于MacOSX用户可以选择dmg用户,下载后安装,按照提示完成安装操作

    然后打开Terminal配置相关的环境:

    装后直接在命令行输入mysql会提示mysql: command not found,需要先将其添加到环境变量:

    1
    vim ~/.bash_profile

    添加以下指令:

    1
    export PATH=${PATH}:/usr/local/mysql/bin

    保存后立即使其生效:

    1
    source ~/.bash_profile

    MySQL管理

    登陆MySQL

    1
    mysql -u root -p

    USE 数据库名 :

    选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。

    1
    2
    mysql> use Mysql;
    Database changed

    SHOW DATABASES:

    列出 MySQL 数据库管理系统的数据库列表。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | date |
    | information_schema |
    | mysql |
    | performance_schema |
    | RUNOOB |
    | sys |
    +--------------------+
    6 rows in set (0.02 sec)

    SHOW TABLES:

    显示指定数据库的所有表,使用该命令前需要使用 use 命令来选择要操作的数据库。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> use date;
    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_date |
    +----------------+
    | runoob_tbl |
    +----------------+
    1 row in set (0.00 sec)

    SHOW COLUMNS FROM 数据表:

    显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。

    SHOW INDEX FROM 数据表:

    显示数据表的详细索引信息,包括PRIMARY KEY(主键)。

    SHOW TABLE STATUS LIKE [FROM db_name] [LIKE ‘pattern’] G:

    该命令将输出Mysql数据库管理系统的性能及统计信息。

    数据库操作

    创建数据库

    我们可以在登陆 MySQL 服务后,使用 create 命令创建数据库,语法如下:

    1
    CREATE DATABASE 数据库名;

    删除数据库

    使用 root 用户登录,root 用户拥有最高权限。
    在删除数据库过程中,务必要十分谨慎,因为在执行删除命令后,所有数据将会消失。
    在删除过程中,我们使用drop命令进行删除数据库的操作:

    1
    drop database 数据库名;

    选择数据库

    在MySQL中可以使用use命令实现当前操作数据库的选择

    1
    2
    mysql> use Learn;
    Database changed

    MySQL数据类型

    整数

    *对于MySQL3.23 以前的版本,DECIMAL(M, D) 列的每个值占用M 字节,而符号(如果需要)和小数点包括在M 字节中。因此,类型为DECIMAL(5, 2) 的列,其取值范围为-9.99 到9 9 . 9 9

    而在MySQL3.23 之后的版本中,DECIMAL 值是根据ANSI 规范进行处理的, ANSI 规范规定DECIMAL(M, D) 必须能够表示M 位数字及D 位小数的任何值。

    日期和时间类型

    *TIMESTAMP有自动更新的特性

    字符串类型

    CHAR 和 VARCHAR 类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。

    BINARY 和 VARBINARY 类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。

    BLOB 是一个二进制大对象,可以容纳可变数量的数据。有 4 种 BLOB 类型:TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们区别在于可容纳存储范围不同。

    有 4 种 TEXT 类型:TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。对应的这 4 种 BLOB 类型,可存储的最大长度不同,可根据实际情况选择。

    (此内容来自http://www.runoob.com/mysql/mysql-data-types.html)

    数据表操作

    创建数据表

    创建MySQL数据表需要以下信息:

    • 表名
    • 表字段名
    • 定义每个表字段

    创建数据表的通用语法为:

    1
    CREATE TABLE table_name (column_name column_type);

    实例:

    1
    2
    3
    4
    5
    6
    7
    mysql> create table table1(
    -> num_id int not null auto_increment,
    -> num_title varchar(100) not null,
    -> num_author varchar(30) not null,
    -> mission_date datetime,
    -> primary key (num_id)
    -> )engine=InnoDB default charset=utf8;

    *在命令的最后一行出现了
    engine
    选项,以及default选项,可以通过连接的网站具体了解!

    *关于其中not null 和 null选项可以通过null or not null进行具体的了解

    可以通过show columns from table1查看创建的结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> show columns from table1;
    +--------------+--------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +--------------+--------------+------+-----+---------+----------------+
    | num_id | int(11) | NO | PRI | NULL | auto_increment |
    | num_title | varchar(100) | NO | | NULL | |
    | num_author | varchar(30) | NO | | NULL | |
    | mission_date | datetime | YES | | NULL | |
    +--------------+--------------+------+-----+---------+----------------+

    MySQL 删除数据表

    MySQL中删除数据表是非常容易操作的,进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失。

    1
    drop table table_name;

    关于删除操作的几点补充:

    删除表内数据使用delete命令

    1
    2
    3
    4
    delete from 表名 where 删除条件;
    e.g
    delete from table1 where num_author = "Alice";
    实例:删除table1表内num_author为Alice的记录。

    另外,在执行delete操作之后,可以使用命令optimize table table_name将删除的磁盘空间释放

    清除表内数据但是保存表结构truncate命令

    1
    2
    3
    4
    truncate table 表名;
    e.g.
    truncate table table1;
    清楚表table1内的全部数据

    MySQL插入数据

    MySQL 表中使用 INSERT INTO SQL语句来插入数据。
    插入的具体语法为:

    1
    2
    3
    INSERT INTO table_name ( field1, field2,...fieldN )
    VALUES
    ( value1, value2,...valueN );

    特别地,如果数据是字符型,那么请使用''或者"",如"value1"

    实例:向table1表中插入一行数据

    1
    2
    3
    4
    5
    6
    7
    mysql> insert into table1
    -> (num_title,num_author,mission_date)
    -> values
    -> ("MySQL","Chaotic",now());
    Query OK, 1 row affected (0.09 sec)

    *NOW() 是一个 MySQL 函数,该函数返回日期和时间。

    使用select * from <table_name>命令查看数据表中的内容可以看到:

    1
    2
    3
    4
    5
    6
    7
    mysql> select * from table1;
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaotic | 2018-10-16 14:38:08 |
    +--------+-----------+------------+---------------------+
    1 row in set (0.01 sec)

    MySQL中查询数据

    MySQL 数据库使用SQL SELECT语句来查询数据

    语法:

    1
    2
    3
    4
    SELECT column_name,column_name
    FROM table_name
    [WHERE Clause]
    [LIMIT N][ OFFSET M]
    • 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
    • 可以使用*来 代替命令中的选项,select会返回表中所有的字段数据
    • where语句中可以包含任何你想要的条件
    • limit可以设定返回值的数量
    • offset可以设置偏移量,默认为0

    实例

    使用select * from <table_name>命令查看数据表中的内容可以看到:

    1
    2
    3
    4
    5
    6
    7
    mysql> select * from table1;
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaotic | 2018-10-16 14:38:08 |
    +--------+-----------+------------+---------------------+
    1 row in set (0.01 sec)

    where字句

    如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中

    在where字句中:

    • 指定任何条件
    • 使用 AND 或者 OR 指定一个或多个条件
    • 子句也可以运用于 SQL 的 DELETE 或者 UPDATE 命令
    • 类似于程序语言中的 if 条件,根据 MySQL 表中的字段值来读取指定的数据,可以使用 = != > < >= <=等逻辑运算符号

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    mysql> select * from table1 where num_author='Chaotic';
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaotic | 2018-10-16 14:38:08 |
    +--------+-----------+------------+---------------------+
    1 row in set (0.00 sec)

    mysql> select * from table1 where num_author='Arron';
    Empty set (0.01 sec)

    MySQL UPDATE字句

    修改或更新 MySQL 中的数据,使用 SQL UPDATE 命令来操作。

    语法:

    1
    2
    UPDATE table_name SET field1=new-value1, field2=new-value2
    [WHERE Clause]

    对于UPDATE命令,有以下的限定条件:

    • 同时更新1个或者多个字段
    • 可以在where中限定任何条件
    • 在一个单独表中同时更新数据

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    原始表格:
    mysql> select * from table1;
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaotic | 2018-10-16 14:38:08 |
    | 2 | C++ | Arron | 2018-10-16 15:00:06 |
    | 3 | Python | Alice | 2018-10-16 15:00:20 |
    +--------+-----------+------------+---------------------+

    使用UPDATE命令:
    mysql> update table1 set num_author="Bob" where num_id =3;

    得到结果:
    mysql> select * from table1;
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaotic | 2018-10-16 14:38:08 |
    | 2 | C++ | Arron | 2018-10-16 15:00:06 |
    | 3 | Python | Bob | 2018-10-16 15:00:20 |
    +--------+-----------+------------+---------------------+

    使用UPDATE改变其中的某几个字符串:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    update <table_name> set <filed>=replace(filed,'value1','value2') where clause;
    例如:
    mysql> update table1 set num_author=replace(num_author,'tic','s') where num_id = 1;
    结果:

    mysql> select * from table1; +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaos | 2018-10-16 14:38:08 |
    | 2 | C++ | Arron | 2018-10-16 15:00:06 |
    | 3 | Python | Bob | 2018-10-16 15:00:20 |
    +--------+-----------+------------+---------------------+

    Like语句

    我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录。
    WHERE 子句中 = 来设定条件,如 “num_author = ‘RUNOOB.COM’”。

    但是当需要获取 num_author 字段含有 “a” 字符的所有记录,这时我们就需要在 WHERE 子句中使用 SQL LIKE 子句。

    SQL LIKE 子句中使用百分号 %字符来表示任意字符,类似于UNIX或正则表达式中的星号 *。
    如果没有使用百分号 %, LIKE 子句与等号 = 的效果是一样的。

    实例:

    1
    2
    3
    4
    5
    6
    mysql> select * from table1 where num_author like 'Cha%';
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 1 | MySQL | Chaos | 2018-10-16 14:38:08 |
    +--------+-----------+------------+---------------------+

    补充,%_的配合使用

    1
    2
    3
    4
    5
    6
    '%a'     //以a结尾的数据
    'a%' //以a开头的数据
    '%a%' //含有a的数据
    '_a_' //三位且中间字母是a的
    '_a' //两位且结尾字母是a的
    'a_' //两位且开头字母是a的

    MySQL UNION操作符

    MySQL UNION 操作符用于连接2个以上的 SELECT 语句的结果组合到一个结果集合中。其中多个 SELECT语句会删除重复的数据。

    其语法为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    SELECT column_name1, column_name2, ... column_name_n
    FROM tables
    [WHERE conditions]
    UNION [ALL | DISTINCT]
    SELECT column_name1, column_name2, ... column_name_n
    FROM tables
    [WHERE conditions];

    其中:
    1、DISTINCT:删除结果集中重复的数据
    2、ALL:返回所有结果,包含重复数据

    MySQL UNION操作符

    MySQL UNION 操作符用于连接2个以上的 SELECT 语句的结果组合到一个结果集合中。其中多个 SELECT语句会删除重复的数据。

    其语法为:

    1
    2
    3
    4
    5
    6
    7
    8
    9大专栏  Mysql简单总结n>
    10
    11
    SELECT column_name1, column_name2, ... column_name_n
    FROM tables
    [WHERE conditions]
    UNION [ALL | DISTINCT]
    SELECT column_name1, column_name2, ... column_name_n
    FROM tables
    [WHERE conditions];

    其中:
    1、DISTINCT:删除结果集中重复的数据
    2、ALL:返回所有结果,包含重复数据

    排序

    从 MySQL 表中使用 SQL SELECT 语句来读取数据。

    如果我们需要对读取的数据进行排序,我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。

    1
    SELECT * from <table_name> ORDER BY <column_name>;

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    mysql> select * from table1 order by num_author;
    +--------+-----------+------------+---------------------+
    | num_id | num_title | num_author | mission_date |
    +--------+-----------+------------+---------------------+
    | 2 | C++ | Arron | 2018-10-16 15:00:06 |
    | 3 | Python | Bob | 2018-10-16 15:00:20 |
    | 1 | MySQL | Chaos | 2018-10-16 14:38:08 |
    +--------+-----------+------------+---------------------+

    连接的使用

    在实际的应用中经常需要从多个数据表中读取数据。 MySQL 的 JOIN可以在两个或多个表中查询数据。可以在 SELECT, UPDATEDELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。

    JOIN 按照功能大致分为如下三类:

    • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
    • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
    • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

    INNER JOIN

    得到两个表相同的地方:

    1
    2
    语法
    SELECT a.column_name1, a.column_name2, ... b.column_name1, b.column_name2,... from <table_name1> a INNER JOIN <table_name2> b ON a.column_nameX = b.columen_nameY

    实例

    我们先新建一个表:table2

    1
    2
    3
    4
    mysql> create table table2(
    -> num_author varchar(100) not null,
    -> num_admi int not null
    -> )engine = InnoDB default charset=utf8;

    添加两条记录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    mysql> insert into table2(num_author, num_admi)
    -> values
    -> ("Arron",10);
    Query OK, 1 row affected (0.07 sec)

    mysql> insert into table2(num_author, num_admi)
    -> values
    -> ("Bob",17);
    Query OK, 1 row affected (0.05 sec)

    mysql> select * from table2;
    +------------+----------+
    | num_author | num_admi |
    +------------+----------+
    | Arron | 10 |
    | Bob | 17 |
    +------------+----------+

    使用inner join命令

    1
    2
    3
    4
    5
    6
    7
    mysql> select a.num_id, a.num_author, b.num_admi from table1 a inner join table2 b on a.num_author = b.num_author;
    +--------+------------+----------+
    | num_id | num_author | num_admi |
    +--------+------------+----------+
    | 2 | Arron | 10 |
    | 3 | Bob | 17 |
    +--------+------------+----------+

    Left Join

    选择左侧的表+两个表相同的地方,如果有相同的部分则只显示一次

    语法

    1
    2
    语法
    SELECT a.column_name1, a.column_name2, ... b.column_name1, b.column_name2,... from <table_name1> a Left JOIN <table_name2> b ON a.column_nameX = b.columen_nameY

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    mysql> select a.num_id, a.num_author, b.num_admi from table1 a left join table2 b on a.num_author = b.num_author;
    +--------+------------+----------+
    | num_id | num_author | num_admi |
    +--------+------------+----------+
    | 2 | Arron | 10 |
    | 3 | Bob | 17 |
    | 1 | Chaos | NULL |
    +--------+------------+----------+

    Right Join

    选择右侧的表+两个表相同的地方,如果有相同的部分则只显示一次

    语法

    1
    2
    语法
    SELECT a.column_name1, a.column_name2, ... b.column_name1, b.column_name2,... from <table_name1> a Right JOIN <table_name2> b ON a.column_nameX = b.columen_nameY

    实例

    1
    2
    3
    4
    5
    6
    7
    mysql> select a.num_id, a.num_author, b.num_admi from table1 a right join table2 b on a.num_author = b.num_author;
    +--------+------------+----------+
    | num_id | num_author | num_admi |
    +--------+------------+----------+
    | 2 | Arron | 10 |
    | 3 | Bob | 17 |
    +--------+------------+----------+

    MySQL ALTER

    当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令。

    首先创建一个新的表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    mysql> create table table3(
    -> i int,
    -> c char(1)
    -> );

    mysql> show columns from table3;
    +-------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | i | int(11) | YES | | NULL | |
    | c | char(1) | YES | | NULL | |
    +-------+---------+------+-----+---------+-------+

    对表字段的操作

    使用ALTERDROP删除表字段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    mysql> alter table table3 drop i;
    Query OK, 0 rows affected (0.16 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | c | char(1) | YES | | NULL | |
    +-------+---------+------+-----+---------+-------+

    使用ALTERADD命令添加表字段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    mysql> alter table table3 add i int;
    Query OK, 0 rows affected (0.09 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | c | char(1) | YES | | NULL | |
    | i | int(11) | YES | | NULL | |
    +-------+---------+------+-----+---------+-------+

    *如果你需要指定新增字段的位置,可以使用关键字 FIRST (设定位第一列), AFTER 字段名(设定位于某个字段之后)。

    对字段的操作

    使用ALTERMODIFY修改字段的类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    mysql> show columns from table3;
    +-------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | c | char(1) | YES | | NULL | |
    | i | int(11) | YES | | NULL | |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec)

    mysql> alter table table3 modify c varchar(100);
    Query OK, 0 rows affected (0.11 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | i | int(11) | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+

    使用ALTERCHANGE修改名字和类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | i | int(11) | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)

    mysql> alter table table3 change i j tinyint;
    Query OK, 0 rows affected (0.05 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | j | tinyint(4) | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+

    对字段默认值的操作

    使用ALTER修改字段默认值

    语法

    1
    alter table <table_name> alter <row_name1> set <column_name1> <修改值>

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | j | int(11) | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)

    mysql> alter table table3 alter j set default 1000;
    Query OK, 0 rows affected (0.02 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | j | int(11) | YES | | 1000 | |
    +-------+--------------+------+-----+---------+-------+

    使用ALTERDROP删除字段默认值

    语法

    1
    alter table <table_name> alter <row_name1> drop <column_name1>

    实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | j | int(11) | YES | | 1000 | |
    +-------+--------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)

    mysql> alter table table3 alter j drop default;
    Query OK, 0 rows affected (0.06 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> show columns from table3;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | c | varchar(100) | YES | | NULL | |
    | j | int(11) | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+

    对表名的操作

    语法

    1
    alter table <table_name> rename to <change_name>;

    索引

    创建索引时,你需要确保该索引是应用在 SQL 查询语句的条件(一般作为 WHERE 子句的条件)。

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

    普通索引

    创建索引

    • 使用MySQL命令创建
    1
    2
    3
    mysql> create index index1 on table1(num_author(30));
    Query OK, 0 rows affected (0.03 sec)
    Records: 0 Duplicates: 0 Warnings: 0
    • 修改表结构创建
    1
    2
    3
    mysql> alter table table2 add index index2(num_author);
    Query OK, 0 rows affected (0.09 sec)
    Records: 0 Duplicates: 0 Warnings: 0
    • 创建表的时候直接指明
    1
    2
    3
    4
    5
    6
    mysql> create table table4( 
    -> num_id int not null,
    -> num_name varchar(30) not null,
    -> index index4 (num_name(30))
    -> );
    Query OK, 0 rows affected (0.03 sec)

    删除索引

    1
    drop index <index_name> on <table_name>;

    唯一索引

    在普通索引的命令1中,在index前加上unique即可
    在命令2和命令3中,将index替换为unique即可。

    显示索引信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    show index from <table_name>

    mysql> show index from table1;
    +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
    +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
    | table1 | 0 | PRIMARY | 1 | num_id | A | 2 | NULL | NULL | | BTREE | | | YES |
    | table1 | 1 | index1 | 1 | num_author | A | 3 | NULL | NULL | | BTREE | | | YES |
    +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+

    SQL注入

    我决定另外用一个单独的文章介绍这部分的内容 :)

    请大家帮我测试!
    链接:https://pan.baidu.com/s/1FU22qe1xNk88_FyJ8wmh4w 密码:4p9f

  • 相关阅读:
    realplayer web播放器控件参数和函数
    几种技术语言简介!
    电子书标志设计,精品设计,形象设计,封面设计,宣传广告设计作品欣赏
    QuickCHM2.6出现了"不支持此接口"
    svchost.exe占用CPU 100%的解决方法
    [转]网站健康检查
    php新帮手 PHPMaker v5.0.1.0
    【OpenGL】理解GL_TRIANGLE_STRIP等绘制三角形序列的三种方式
    UML用例图总结
    【转】Ogre的八叉树场景管理器OctreeSceneManager
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12327493.html
Copyright © 2011-2022 走看看