mysql-uroot -poldboy123 -S/data/3306/mysql.sock
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.36 |
+-----------+
mysql>select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
mysql> create database oldboy2 character set gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| oldboy2 |
| performance_schema |
| world |
+--------------------+
mysql> show create database oldboy;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| oldboy |CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
grant all on oldboy2.* to oldboy2@‘localhost‘identified by ‘123‘
mysql>show grants for oldboy2@‘localhost‘;
+----------------------------------------------------------------------------------------------------------------+
| Grants for oldboy2@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘oldboy2‘@‘localhost‘IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ |
| GRANT ALL PRIVILEGES ON `oldboy2`.* TO‘oldboy2‘@‘localhost‘ |
+----------------------------------------------------------------------------------------------------------------+
mysql>select user,host from mysql.user;
+---------+------------+
| user |host |
+---------+------------+
| oldboy1 | 10.0.0.% |
| root |127.0.0.1 |
| oldboy |172.16.1.% |
| oldboy2 | localhost |
| root |localhost |
+---------+------------+
mysql> use oldboy2
mysql> use oldboy2
Database changed
mysql> create table test(
-> id int(4),
-> name varchar(16)
-> ) engine=innodb default charset=gbk;
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+-------------------+
| Tables_in_oldboy2 |
+-------------------+
| test |
+-------------------+
mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id |int(4) | YES | |NULL | |
| name |varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql>show columns from test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id |int(4) | YES | |NULL | |
| name |varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql>show create table test;
+-------+-------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------+
| test |CREATE TABLE `test` (
`id`int(4) DEFAULT NULL,
`name`varchar(16) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-------------------------------------------------------------------------------------------------------------------------+
mysql> insert into test values(1,‘oldboy‘);
mysql> select * from test;
+------+--------+
| id |name |
+------+--------+
| 1 |oldboy |
+------+--------+
mysql> insert into test values(2,‘老男孩‘),(3,‘etiantian‘);
mysql> select * from test ;
+------+-----------+
| id |name |
+------+-----------+
| 1 |oldboy |
| 2 | 老男孩 |
| 3 |etiantian |
+------+-----------+
mysql>select * from test
-> ;
+------+-----------+
| id |name |
+------+-----------+
| 1 |oldboy |
| 2 | 老男孩 |
| 3 |etiantian |
+------+-----------+
mysql> select * from test where name=‘oldboy‘;
+------+--------+
| id |name |
+------+--------+
| 1 |oldboy |
+------+--------+
mysql>select * from test where id>1;
+------+-----------+
| id |name |
+------+-----------+
| 2 | 老男孩 |
| 3 |etiantian |
+------+-----------+
updatetest set name=‘oldgirl‘ where id=1;
mysql>select * from test;
+------+-----------+
| id |name |
+------+-----------+
| 1 |oldgirl |
| 2 | 老男孩 |
| 3 |etiantian |
+------+-----------+
mysql>alter table test add age tinyint(2)after id;
mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id |int(4) | YES | |NULL | |
| age |tinyint(2) | YES | |NULL | |
| name |varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysqldump –B –compact oldboy > /opt/bak.sql
delete from rest;
truncate table test;
drop table test;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> drop database oldboy;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
mysql -uroot -poldboy123 -S/data/3306/mysql.sock</opt/bak.sql
mysql> mysql> show variables like‘character_set%‘;
+--------------------------+-------------------------------------------+
| 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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir |/application/mysql-5.6.36/share/charsets/ |
+--------------------------+-------------------------------------------+
mysql> set global character_set_database =utf8;
mysql> show variables like ‘character_set%‘;
+--------------------------+-------------------------------------------+
| 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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir |/application/mysql-5.6.36/share/charsets/ |
+--------------------------+-------------------------------------------+
[root@mysql data]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
mysql> desc oldboy.test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id |int(4) | YES | |NULL | |
| name |varchar(16) | YES | | NULL | |
| age |int(4) | YES | |NULL | |
+-------+-------------+------+-----+---------+-------+
alter table oldboy.test add primarykey(id);
方法一:alter table oldboy,test addindexindex_name(name)
方法二:create index index_name onoldboy.test(name);
mysql> alter table test add shou char(11) aftername;
mysql> desc test;
+--------+-------------+------+-----+---------+-------+
| Field |Type | Null | Key | Default |Extra |
+--------+-------------+------+-----+---------+-------+
| id |int(4) | YES | MUL | NULL | |
| name |varchar(16) | YES | | NULL | |
| shou |char(11) | YES | |NULL | |
| age |int(4) | YES | |NULL | |
| shouji | char(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
mysql> insert into test values(6,‘mysql‘,‘mei‘,‘21‘,‘1794225527‘),(7,‘mysql1‘,‘mein‘,‘21‘,‘15095,‘15095445967‘);
mysql> select * from test;
+------+---------+------+------+-------------+
| id |name | shou | age | shouji |
+------+---------+------+------+-------------+
| 1 |oldboy | NULL | 21 | NULL |
| 3 |oldboy3 | NULL | 21 | NULL |
| 4 |oldboy4 | NULL | 21 | NULL |
| 5 | 小明 | NULL | 21 | NULL |
| 6 |mysql | mei | 21| 1794225527 |
| 7 |mysql1 | mein | 21 | 15095445967 |
+------+---------+------+------+-------------+
mysql> alter table test add index index_shouji(shouji(8));
mysql> desc test;
+--------+-------------+------+-----+---------+-------+
| Field |Type | Null | Key | Default |Extra |
+--------+-------------+------+-----+---------+-------+
| id |int(4) | YES | MUL | NULL | |
| name |varchar(16) | YES | | NULL | |
| shou |char(11) | YES | |NULL | |
| age |int(4) | YES | | NULL | |
| shouji | char(11) | YES | MUL | NULL | |
+--------+-------------+------+-----+---------+-------+
mysql>mysql> show index from test\G
*************************** 1. row ***************************
Table: test
Non_unique: 1
Key_name: name_idx
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row***************************
Table: test
Non_unique: 1
Key_name: index_shouji
Seq_in_index: 1
Column_name: shouji
Collation: A
Cardinality: 6
Sub_part: 8
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec
mysql> alter table test drop index index_shouji;
mysql> show index from test\G
*************************** 1. row***************************
Table: test
Non_unique: 1
Key_name: name_idx
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
alter table test add indexindex_name_shouji(name(6),shouji(8));
mysql> show index from test\G
*************************** 1. row***************************
Table: test
Non_unique: 1
Key_name: name_idx
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row***************************
Table: test
Non_unique: 1
Key_name: index_name_shouji
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 6
Sub_part: 6
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row***************************
Table: test
Non_unique: 1
Key_name: index_name_shouji
Seq_in_index: 2
Column_name: shouji
Collation: A
Cardinality: 6
Sub_part: 8
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.00 sec)
mysql> select * from test where name=‘mysql‘and shouji like ‘179%‘;
+------+-------+------+------+------------+
| id |name | shou | age | shouji |
+------+-------+------+------+------------+
| 6 |mysql | mei | 21 | 1794225527 |
+------+-------+------+------+------------+
mysql> explain select * from test wherename=‘oldboy‘ and shouji like ‘1%‘;
+----+-------------+-------+-------+-------------------+-------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+-------------------+---------+------+------+-------------+
| 1 |SIMPLE | test | range | index_name_shouji |index_name_shouji | 32 | NULL | 1 | Using where |
+----+-------------+-------+-------+-------------------+-------------------+---------+------+------+-------------+
mysql> explain select * from test where name=‘oldboy‘ and shouji like ‘1%‘\G
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: test
type: range
possible_keys: index_name_shouji
key: index_name_shouji
key_len: 32
ref: NULL
rows: 1
Extra: Using where
1 row in set (0.00 sec)
mysql> show create table test\G
*************************** 1. row***************************
Table: test
Create Table: CREATE TABLE `test` (
`id`int(4) DEFAULT NULL,
`name`varchar(16) DEFAULT NULL,
`shou`char(11) DEFAULT NULL,
`age`int(4) DEFAULT NULL,
`shouji`char(11) DEFAULT NULL,
KEY`name_idx` (`id`),
KEY`index_name_shouji` (`name`(6),`shouji`(8))
) ENGINE=InnoDB DEFAULT CHARSET=gbk
mysql> alter table oldboy.test ENGINE=MyISAM;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> show create table oldboy.test\G
*************************** 1. row***************************
Table: test
Create Table: CREATE TABLE `test` (
`id`int(4) NOT NULL DEFAULT‘0‘,
`age`tinyint(2) DEFAULTNULL,
`name`varchar(16) DEFAULTNULL,
`shouji`char(11) DEFAULTNULL,
PRIMARYKEY (`id`),
KEY`index_name_shouji`(`name`(6),`shouji`(8))
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
本文出自 “linux基础” 博客,请务必保留此出处http://13131277.blog.51cto.com/13121277/1983886
原文地址:http://13131277.blog.51cto.com/13121277/1983886