操作MySQL的基本命令有哪些
admin
2023-05-16 19:42:27
0

本篇文章给大家主要讲的是关于操作MySQL的基本命令有哪些的内容,感兴趣的话就一起来看看这篇文章吧,相信看完操作MySQL的基本命令有哪些对大家多少有点参考价值吧。

操作系统:CentOS

数据库系统:MySQL

[root@sky9896~]# mysql -h localhost  -P  3306 -uroot –p    #注意端口号前的P是大小

mysql>select version(),current_date;   #检索当前数据库版本和当前时间

+----------------+--------------+

|version()      | current_date |

+----------------+--------------+

|5.5.52-cll-lve | 2017-08-09   |

+----------------+--------------+

1row in set (0.00 sec)

mysql>use employees   #打开数据库

Databasechanged

mysql>select database();   #查询当前数据库

+------------+

|database() |

+------------+

|employees  |

+------------+

1row in set (0.00 sec)

mysql>show tables;    #显示该数据库当中的所有表

+---------------------+

|Tables_in_employees |

+---------------------+

|departments         |

|dept_emp            |

|dept_manager        |

|employees           |

|salaries            |

|titles              |

+---------------------+

6rows in set (0.00 sec)

 mysql>create table employees_2(        #建表

    -> emp_no int(11) NOT NULL,

    -> birth_date date NOT null,

    -> first_name varchar(14) not null,

    -> last_name varchar(16) not null,

    -> gender enum('M','F') not null,

    -> hire_date date not null,

    -> primary key(emp_no)

    -> )engine=innodb default charset=latin1;

QueryOK, 0 rows affected (0.19 sec)

mysql>desc employees_2;      #显示表结构

+------------+---------------+------+-----+---------+-------+

|Field      | Type          | Null | Key | Default | Extra |

+------------+---------------+------+-----+---------+-------+

|emp_no     | int(11)       | NO  | PRI | NULL    |      |

|birth_date | date          | NO   |    | NULL    |       |

|first_name | varchar(14)   | NO   |    | NULL    |       |

|last_name  | varchar(16)   | NO  |     | NULL    |      |

|gender     | enum('M','F') | NO   |    | NULL    |       |

|hire_date  | date          | NO  |     | NULL    |      |

+------------+---------------+------+-----+---------+-------+

6rows in set (0.02 sec)

mysql>create table t1(a integer,b char(10));

QueryOK, 0 rows affected (0.11 sec)

mysql>show tables;

+---------------------+

|Tables_in_employees |

+---------------------+

|departments         |

|dept_emp            |

|dept_manager        |

|employees           |

|employees_2         |

|salaries            |

|t1                  |

|titles              |

+---------------------+

8rows in set (0.00 sec)

mysql>alter table t1 rename t2;    #修改表名

QueryOK, 0 rows affected (0.05 sec)

mysql>show tables;

+---------------------+

|Tables_in_employees |

+---------------------+

|departments         |

|dept_emp            |

|dept_manager        |

|employees           |

|employees_2         |

|salaries            |

|t2                  |

|titles              |

+---------------------+

8rows in set (0.00 sec)

mysql>desc t2;                                                     

+-------+----------+------+-----+---------+-------+

|Field | Type     | Null | Key | Default |Extra |

+-------+----------+------+-----+---------+-------+

|a     | int(11)  | YES |     | NULL    |      |

|b     | char(10) | YES  |     |NULL    |       |

+-------+----------+------+-----+---------+-------+

2rows in set (0.00 sec)

 

mysql>  alter table t2 modify a tinyint notnull,change b c char(20);   #修改列表

QueryOK, 0 rows affected (0.25 sec)

Records:0  Duplicates: 0  Warnings: 0

 

mysql>desc t2;

+-------+------------+------+-----+---------+-------+

|Field | Type       | Null | Key | Default| Extra |

+-------+------------+------+-----+---------+-------+

|a     | tinyint(4) | NO   |    | NULL    |       |

|c     | char(20)   | YES |     | NULL    |      |

+-------+------------+------+-----+---------+-------+

2rows in set (0.00 sec)

 

ysql>alter table t2 add d timestamp;

QueryOK, 0 rows affected (0.19 sec)

Records:0  Duplicates: 0  Warnings: 0

 

mysql>desc t2;

+-------+------------+------+-----+-------------------+-----------------------------+

|Field | Type       | Null | Key |Default           | Extra                       |

+-------+------------+------+-----+-------------------+-----------------------------+

|a     | tinyint(4) | NO   |    | NULL              |                             |

|c     | char(20)   | YES |     | NULL              |                             |

|d     | timestamp  | NO  |     | CURRENT_TIMESTAMP | on updateCURRENT_TIMESTAMP |

+-------+------------+------+-----+-------------------+-----------------------------+

3rows in set (0.00 sec)

 

mysql>alter table t2 add index(d),add index(a);   #添加索引

mysql>show keys from t2;    

 

mysql>insert into employees(emp_no,birth_date,first_name,last_name,gender,hire_date)values('1111112','2017-8-9','wu','haiming','M','2017-08-09');

QueryOK, 1 row affected (0.09 sec)

mysql>select * from employees where emp_no=1111112;

+---------+------------+------------+-----------+--------+------------+

|emp_no  | birth_date | first_name |last_name | gender | hire_date  |

+---------+------------+------------+-----------+--------+------------+

|1111112 | 2017-08-09 | wu         |haiming   | M      | 2017-08-09 |

+---------+------------+------------+-----------+--------+------------+

1row in set (0.00 sec)

 以上关于操作MySQL的基本命令有哪些详细内容,对大家有帮助吗?如果想要了解更多相关,可以继续关注我们的行业资讯板块。

相关内容

热门资讯

8年后回应姗姗来迟,真相还有人... 八年了,李小璐想起来回应“做头发”事件了。而且这次,她一次性抛出了不少新信息,表示广为流传的“做头发...
男子在攀岩馆从高空坠落,目击者... 8月2日中午,北京大型攀岩馆超级奥攀,一名男子从高空坠落,随后被送医治疗,多名网友发帖留言称确有此事...
小红书黑红启示录:旅游帖诈骗冲... #小红书旅游帖诈骗#冲上了热搜。一位妈妈带孩子在大连,沿着小红书攻略找那条海底隧道——满心想着给孩子...
天然气灶不打火怎么回事 可能由以下原因导致天然气灶不打火:1、燃气阀门未开启:检查气阀是否处于关闭状态。2、灶具没有点火:可...
空调遥控器按开关没反应 原因可能是遥控器里面的发光管损坏,导致屏幕不显示,可以维修或更换遥控器解决;原因可能是遥控器的红外发...
油烟机开关按了没反应 油烟机开关按了没反应油烟机开关按了没反应的原因有可能是开关按钮损坏;电源接头没有连接;电路板损坏、传...
马桶水箱按钮按不动 有因有:马桶按钮内的弹簧生锈老化;出水阀被泥沙等杂物卡住;水漂卡住了。需打开马桶水箱看看联动冲水阀连...
德意油烟机按不动怎么办 如果德意油烟机按不动,首先需要确认是否是电源问题。你可以检查油烟机的插头是否插紧了,插座是否正常工作...
台湾民众党已不是一人政党!柯文... 海峡导报综合报道 台湾民众党2日举办7周年党庆,创党主席柯文哲上台致辞时获得在场人士欢呼,声音久久不...
男子买到破损游泳“跟屁虫”,退... “几十块钱的东西还保你啥子命?这就是一个玩具!”7月22日,成都市民李先生向红星新闻反映,自己周末准...