设为首页 收藏本站
查看: 540|回复: 0

[经验分享] mysql语句的基本操作

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-15 12:43:04 | 显示全部楼层 |阅读模式
实践练习环境:直接在生产环境中操作
OS:CentOS6.8
具体操作流程如下:
Last login: Wed Aug 10 08:07:15 2016 from ********
欢迎登录*************CentOS服务器!
[sky@sky9896 ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 114094
Server version: 5.5.49-cll-lve MySQL Community Server (GPL) by Atomicorp
Copyright (c) 2000, 2016, 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> create database a  #创建数据库a
    -> ;
Query OK, 1 row affected (0.08 sec)
mysql> show databases;  #显示所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| back20150625ultrax |
| cacti              |
| cacti20151220      |
| cacti20160104      |
| feifeicms          |
| mysql              |
| performance_schema |
| phpcom             |
| study              |
| syslog             |
| test               |
| test1              |
| tt                 |
| ultrax             |
+--------------------+
16 rows in set (0.16 sec)
mysql> use a   #打开数据库
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table a1(id int);  #创建a1表
Query OK, 0 rows affected (0.22 sec)
mysql> show tables;   #显示所有表;
+-------------+
| Tables_in_a |
+-------------+
| a1          |
+-------------+
1 row in set (0.00 sec)
mysql> describe a1  #显示表结构
    -> ;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> show engines; #查看引擎
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | 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         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
[sky@sky9896 ~]$ mysql -u root-p  #连接数据库
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 114209
Server version: 5.5.49-cll-lve MySQL Community Server (GPL) by Atomicorp
Copyright (c) 2000, 2016, 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> exit   #退出
Bye
mysql> use a;
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> create table a2(
    -> id int unsigned not null  auto_increment, #不能为空,自动增加数值
    -> name char(40) not null default ' ', #不能为空,默认为空
    -> info char(200) null,
    -> primary key(id));#设置id为主键
Query OK, 0 rows affected (0.12 sec)
mysql> describe a2;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(40)         | NO   |     |         |                |
| info  | char(200)        | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| a1          |
| a2          |
+-------------+
2 rows in set (0.00 sec)
mysql> insert into a2(id,name,info)values('1','wuhaiming','参加项管考试 ');  #向a2表插入一条记录
Query OK, 1 row affected, 1 warning (0.07 sec)
mysql> select * from a2;
+----+-----------+--------+
| id | name      | info   |
+----+-----------+--------+
|  1 | wuhaiming | ?????? |
+----+-----------+--------+
1 row in set (0.00 sec)
mysql> select  id from a2;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
mysql> insert into a2 values(2,'a2','sky9890');
Query OK, 1 row affected (0.04 sec)
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ??????  |
|  2 | a2        | sky9890 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> insert into a2(id,name)values('','whm');
Query OK, 1 row affected, 1 warning (0.06 sec)
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ??????  |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
+----+-----------+---------+
3 rows in set (0.00 sec)
mysql> describe a2;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(40)         | NO   |     |         |                |
| info  | char(200)        | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into a2 values('','a6','sky9890'),('','a7',);#插入一条记录
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
mysql> insert into a2 values('','a6','sky9890'),('','a7','sky'); #连续插入两条记录
Query OK, 2 rows affected, 2 warnings (0.17 sec)
Records: 2  Duplicates: 0  Warnings: 2
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ??????  |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
|  5 | a7        | sky     |
+----+-----------+---------+
5 rows in set (0.00 sec)
mysql> describe a1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> describe a2;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(40)         | NO   |     |         |                |
| info  | char(200)        | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from a1;
Empty set (0.00 sec)
mysql> insert into a1(id) select id from a2;
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0
mysql> select * from a1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+
5 rows in set (0.00 sec)
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ??????  |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
|  5 | a7        | sky     |
+----+-----------+---------+
5 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| back20150625ultrax |
| cacti              |
| cacti20151220      |
| cacti20160104      |
| feifeicms          |
| mysql              |
| performance_schema |
| phpcom             |
| study              |
| syslog             |
| test               |
| test1              |
| tt                 |
| ultrax             |
+--------------------+
16 rows in set (0.00 sec)
mysql> drop database b; #删除b数据库
ERROR 1008 (HY000): Can't drop database 'b'; database doesn't exist
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| a1          |
| a2          |
+-------------+
2 rows in set (0.00 sec)
mysql> drop tables  a1;#删除a1表
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| a2          |
+-------------+
1 row in set (0.00 sec)
mysql> delete from a2 where id=5 or info='sky'; #删除一条记录
Query OK, 1 row affected (0.05 sec)
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ??????  |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> update a2 set  info='sky' where id=1; #更新一个字段值
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | sky     |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> SET collation_database = utf8 ;
ERROR 1273 (HY000): Unknown collation: 'utf8'
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> update a2 set  info='吴海明' where id=1;         
Query OK, 1 row affected, 1 warning (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> select * from a2;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ???     |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> alter table a2 rename a1 #修改表名
    -> ;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| a1          |
+-------------+
1 row in set (0.00 sec)
mysql> select * from a2;      
ERROR 1146 (42S02): Table 'a.a2' doesn't exist
mysql> select * from a2;
ERROR 1146 (42S02): Table 'a.a2' doesn't exist
mysql> select * from a1;
+----+-----------+---------+
| id | name      | info    |
+----+-----------+---------+
|  1 | wuhaiming | ???     |
|  2 | a2        | sky9890 |
|  3 | whm       | NULL    |
|  4 | a6        | sky9890 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> alter table a1 change info information char(200);#更改字段名
Query OK, 4 rows affected (0.26 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> describe a1;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | char(40)         | NO   |     |         |                |
| information | char(200)        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table a1 add time date;#在末尾填加列
Query OK, 4 rows affected (0.26 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> describe a1;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | char(40)         | NO   |     |         |                |
| information | char(200)        | YES  |     | NULL    |                |
| time        | date             | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table a1 drop time; #删除列
Query OK, 4 rows affected (0.26 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> describe a1;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | char(40)         | NO   |     |         |                |
| information | char(200)        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table a1 add time date first;#插入到第一列
Query OK, 4 rows affected (0.24 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> describe a1;                       
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| time        | date             | YES  |     | NULL    |                |
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | char(40)         | NO   |     |         |                |
| information | char(200)        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from a1;
+------+----+-----------+-------------+
| time | id | name      | information |
+------+----+-----------+-------------+
| NULL |  1 | wuhaiming | ???         |
| NULL |  2 | a2        | sky9890     |
| NULL |  3 | whm       | NULL        |
| NULL |  4 | a6        | sky9890     |
+------+----+-----------+-------------+
4 rows in set (0.00 sec)
mysql> alter table a1 add nian year after time;#插入到指定列后
Query OK, 4 rows affected (0.25 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> describe a1;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| time        | date             | YES  |     | NULL    |                |
| nian        | year(4)          | YES  |     | NULL    |                |
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | char(40)         | NO   |     |         |                |
| information | char(200)        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> grant all on cacti.* to 'a1'@'localhost' identified by '123456';#授权
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye
mysql> grant create,select on *.* to  'a2'@'localhost' identified by '123'; #授权
Query OK, 0 rows affected (0.00 sec)
mysql> revoke select on  *.*  from  'a2'@'localhost'; #撤回权限



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-258111-1-1.html 上篇帖子: mysql的源代码安装 下篇帖子: Mysql修复表结构 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表