mysql语句的基本操作
admin
2023-06-07 09:01:26
0

实践练习环境:直接在生产环境中操作

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'; #撤回权限

 

相关内容

热门资讯

AI热潮下的公关困境:英国企业... IT之家 5 月 24 日消息,据《卫报》今天报道,各大公关企业表示,英国各大公司如今正在想方设法把...
原创 6... 最近手机圈可太热闹了,小米、iQOO、红米、联想一个接一个地发新机,后面还有一大堆在路上,简直就跟下...
伊朗方面披露美伊或将签署的备忘... △伊朗德黑兰(资料图)总台记者24日自伊朗多个消息渠道了解到伊朗与美国或将签署的备忘录草案的部分内容...
京东工业发布AI智采管家 助力... 当前,新一轮科技革命与产业变革加速演进,人工智能正成为推动中国工业转型升级的核心引擎。长期以来,中小...
2026三门峡横渡母亲河活动启... 5月24日,2026年“大江大河”全国群众公开水域游泳系列赛(三门峡站)暨2026中国·三门峡横渡母...
led吸顶灯一半亮一半不亮怎么... 当 LED 吸顶灯一半亮一半不亮时,可能是以下原因导致的:1. LED 灯珠损坏:部分 LED 灯珠...
不让孩子看电视的方法 孩子们在成长过程中,电视是他们的一种娱乐方式,但是看电视时间过长会对孩子的健康和发展产生**影响。所...
奥克斯全自动洗衣自动进水管进水... 1、可能是水位开关性能不良,不能精准的识别水位,引起进水太多导致的。2、可能是溢水管本身损坏造成的。...
全自动洗衣机水管加长方法 对于家庭使用的全自动洗衣机来说,水管长度是需要考虑的重要问题。如果水管长度不够,就会给使用带来很多麻...
全自动洗衣机排水管怎么安装方法 波轮洗衣机排水管的更换方法:1、更换排水管前需要先准备一款新的排水管,更换排水管前不要忘记断开洗衣机...