MySQL必会企业面试题
生活随笔
收集整理的這篇文章主要介紹了
MySQL必会企业面试题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文轉載
老男孩教育
mysql -uroot -poldboy
mysql -uroot -poldboy -S /data/3306/mysql.sock
mysql> select version(); 查看版本 mysql> select user(); 查看用戶
mysql> create database oldboy character set gbk collate gbk_chinese_ci; mysql> show create database oldboy\G
mysql> grant all on oldboy.* to oldboy@’localhost’ identified by ‘123456’; mysql> select user,host from mysql.user;
mysql> grant all on oldboy.* to oldboy@’192.168.1.%/255.255.255.0’ identified by ‘123456’; mysql> select user,host from mysql.user;
mysql> create user ‘abc’@’localhost’ identified by ‘123456’; mysql> grant all on oldboy.* to ‘abc’@’localhost’;
mysql> select user,host from mysql.user;
mysql> use oldboy
mysql> create table test( -> id int(4), -> name varchar(16) -> )ENGINE=innodb default charset=gbk; Query OK, 0 rows affected (0.02 sec)
mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> show columns from test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> show create table test;
mysql> insert into test values(1,'oldboy'); mysql> select * from test; +------+--------+ | id | name | +------+--------+ | 1 | oldboy | +------+--------+ 1 row in set (0.00 sec)
mysql> insert into test values(2,'老男孩'),(3,'etiantian'); Query OK, 2 rows affected (0.07 sec) Records: 2 Duplicates: 0 Warnings: 0 查看創建情況 mysql> select * from test; +------+-----------+ | id | name | +------+-----------+ | 1 | oldboy | | 2 | 老男孩 | | 3 | etiantian | +------+-----------+ 3 rows in set (0.00 sec)
mysql> select * from test; +------+-----------+ | id | name | +------+-----------+ | 1 | oldboy | | 2 | 老男孩 | | 3 | etiantian | +------+-----------+ 3 rows in set (0.00 sec)
mysql> select * from test where name='oldboy'; +------+--------+ | id | name | +------+--------+ | 1 | oldboy | +------+--------+ 1 row in set (0.00 sec)
mysql> select * from test where id>1; +------+-----------+ | id | name | +------+-----------+ | 2 | 老男孩 | | 3 | etiantian | +------+-----------+ 2 rows in set (0.00 sec)
mysql> update test set name='oldgirl' where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test; +------+-----------+ | id | name | +------+-----------+ | 1 | oldgirl | | 2 | 老男孩 | | 3 | etiantian | +------+-----------+ 3 rows in set (0.00 sec)
mysql> alter table test add age tinyint(2) after id; Query OK, 3 rows affected (0.04 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | age | tinyint(2) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysqldump -uroot -poldboy -S /data/3306/mysql.sock --events -B oldboy mysql >/opt/mysql_back.sql cat /opt/mysql_back.sql
第一種mysql> delete from test; 第二種mysql> truncate table test; mysql> select * from test; Empty set (0.00 sec)
mysql> drop table test; 刪除表 mysql> drop database test; 刪除庫
mysql -uroot -poldboy -S /data/3306/mysql.sock </opt/mysql_back.sql
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.5.49/share/charsets/ | 由配置文件設置 +--------------------------+-------------------------------------------+ 8 rows in set (0.00 sec)
/etc/init.d/mysqld stop mysqld_safe --skip-grant-tables --user=mysql & mysql 修改完密碼重啟 /etc/init.d/mysqld restart
/data/3306/mysql stop 無法停止 killall mysqld mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables --user=mysql & update mysql.user set password=password('oldboy456') where user='root' and host='localhost'; flush privileges; mysql 登錄 mysqladmin -uroot -poldboy shutdown /etc/init.d/mysqld start
1. 網站程序字符集 2. 客戶端的字符集 3. 服務器端字符集 4. linux客戶端字符集 5. 以上都要統一,否則會出現中文亂碼 如果編譯的時候指定了特定的字符集,則以后創建對應字符集的數據庫就不需要指定字符集 -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \ 提示:二進制軟件包,安裝的數據庫字符集默認latinl
mysql> alter table test add primary key(id); mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | NO | PRI | 0 | | | age | tinyint(2) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysql> alter table test add index index_name(name); create index index_name on test(name);
mysql> alter table test add shouji char(11) after name; Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc test; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(4) | NO | PRI | 0 | | | age | tinyint(2) | YES | | NULL | | | name | varchar(16) | YES | MUL | NULL | | | shouji | char(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
mysql> insert into test values(4,24,'cyh','604419314'),(5,38,'oldboy','123456'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+------+-----------+-----------+ | id | age | name | shouji | +----+------+-----------+-----------+ | 1 | NULL | oldgirl | NULL | | 2 | NULL | 老男孩 | NULL | | 3 | NULL | etiantian | NULL | | 4 | 24 | cyh | 604419314 | | 5 | 38 | oldboy | 123456 | +----+------+-----------+-----------+ 5 rows in set (0.00 sec)
mysql> alter table test add index index_shouji(shouji(8)); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+------+-----------+-----------+ | id | age | name | shouji | +----+------+-----------+-----------+ | 1 | NULL | oldgirl | NULL | | 2 | NULL | 老男孩 | NULL | | 3 | NULL | etiantian | NULL | | 4 | 24 | cyh | 604419314 | | 5 | 38 | oldboy | 123456 | +----+------+-----------+-----------+ 5 rows in set (0.00 sec)
mysql> show index from test\G *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: test Non_unique: 1 Key_name: index_name Seq_in_index: 1 Column_name: name Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: test Non_unique: 1 Key_name: index_shouji Seq_in_index: 1 Column_name: shouji Collation: A Cardinality: 5 Sub_part: 8 Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec)
alter table test drop index index_name; drop index index_shouji on test; ysql> drop index index_shouji on test; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from test\G *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec
mysql> alter table test add index index_name_shouji(name(6),shouji(8)); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from test\G *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: 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: 5 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: 5 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='cyh' and shouji like '6044%'; +----+------+------+-----------+ | id | age | name | shouji | +----+------+------+-----------+ | 4 | 24 | cyh | 604419314 | +----+------+------+-----------+ 1 row in set (0.00 sec)
mysql> explain select * from test where name='cyh' and shouji like '6044%'\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) NOT NULL DEFAULT '0', `age` tinyint(2) DEFAULT NULL, `name` varchar(16) DEFAULT NULL, `shouji` char(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name_shouji` (`name`(6),`shouji`(8)) ) ENGINE=InnoDB DEFAULT CHARSET=gbk 1 row in set (0.00 sec) 修改后==================================== mysql> alter table test ENGINE=MYISAM; Query OK, 5 rows affected (0.03 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE TEST\G *************************** 1. row *************************** Table: TEST Create Table: CREATE TABLE `test` ( `id` int(4) NOT NULL DEFAULT '0', `age` tinyint(2) DEFAULT NULL, `name` varchar(16) DEFAULT NULL, `shouji` char(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name_shouji` (`name`(6),`shouji`(8)) ) ENGINE=MyISAM DEFAULT CHARSET=gbk 1 row in set (0.00 sec)
原文:http://user.qzone.qq.com/49000448/blog/1427333863
1.登陸數據庫
(1)單實例
(2)多實例
2.查看數據庫版本及當前登錄用戶是什么
3.創建GBK字符集的數據庫oldboy,并查看已建庫的完整語句
4.創建用戶oldboy,使之可以管理數據庫oldboy
第一種方法:
第二種方法:
第三種方法:
6.查看當前數據庫里有哪些用戶。
7.進入oldboy數據庫
8.創建一innodb引擎字符集為GBK表test,字段為id和namevarchar(16),查看建表結構及SQL語句
第一種方法:
第二種方法:
查看表權限
9.插入一條數據 1,oldboy
10.批量插入數據 2,老男孩,3,etiantian。要求中文不能亂碼
11.查詢插入的所有記錄,查詢名字為oldboy的記錄。查詢id大于1的記錄。
(1)第一種方法
(2)第二種方法
(3)第三種方法
12.把數據id等于1的名字oldboy更改為oldgirl
查看修改情況
13.在字段name前插入age字段,類型tinyint(2)
14.備份oldboy庫及MySQL庫
15.刪除表中的所有數據,并查看
16.刪除表test和oldboy數據庫并查看
17.Linux命令行恢復以上刪除的數據
18.把GBK字符集修改為UTF8(可選,注意,此題有陷阱)
解決方法思想:
1、停止MySQL,單例可以使用/etc/init.d/mysqld(編譯的話需要設置,yum安裝就會出現) ??????????? 多實例:/data/3306/mysql shutdown或者是/data/3306/mysql stop 這個需要我們自己寫腳本。官方有參考2、cp /etc/my.cnf /etc/my.cnf.bak,修改前做備份,這是個好習慣。 ????? 修改my.cnf ????? vi /etc/my.cnf ????? 在[client]下添加,client為控制客戶端的,沒試過,沒有的可以不需要加。 ????? default-character-set=utf8 ????? 在[mysqld]下添加,mysqld為控制服務器端的,改過了,OK。 ??? default-character-set=utf8 3.重啟:yum安裝可以使用/etc/init.d/mysqld start 多實例要使用/data/3306/mysql restart(多實例詳細介紹見下一篇文章) 4.show variables like ‘%char%’;查看19.MySQL密碼丟了,如何找回實戰?
單實例
多實例
MySQL內中文數據亂碼的原理及如何防止亂碼?(可選)
21.在把id列設置為主鍵,在Name字段上創建普通索引
添加索引步驟
22.在字段name后插入手機號字段(shouji),類型char(11)
23.所有字段上插入2條記錄(自行設定數據)
24.在手機字段上對前8個字符創建普通索引
25.查看創建的索引及索引類型等信息
26.刪除Name,shouji列的索引
27.對Name列的前6個字符以及手機列的前8個字符組建聯合索引
28.查詢手機號以135開頭的,名字為oldboy的記錄(此記錄要提前插入)
29.查詢上述語句的執行計劃(是否使用聯合索引等)
30.把test表的引擎改成MyISAM
總結
以上是生活随笔為你收集整理的MySQL必会企业面试题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell 脚本比较字符串相等_shel
- 下一篇: MySQL的常见命令