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

相关内容

热门资讯

大疆、影石的“师父”,撑不下去... 一则出售消息,让沉寂已久的GoPro股价突然暴涨。北京时间5月12日一早,这家美国运动相机厂商宣布,...
13岁女孩被闺蜜胁迫卖淫,父亲... 2022年6月,魏潇(化名)从得知时年13岁的女儿遭胁迫卖淫开始,便踏上了维权之路。4年来,胁迫其女...
苹果突然官宣!降到史上最低价! 近日 iPhone手机降价的消息刷屏了 网友纷纷发帖称 “零点确定降价了” “苹果坚挺了8个月终于降...
石化机械获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示石化机械(000852)新获得一项实用新型专利授权,专利名为“...
小区2.82亩绿地“被交易”变... 近日,西安市世纪颐园小区业主向华商报大风新闻反映,小区西侧一块原本规划为公共绿地的土地,在多年前被开...
黄仁勋喝蜜雪冰城,品出了什么味... 每经评论员 朱成祥黄仁勋近期到访北京,被拍到在胡同喝豆汁、吃炸酱面、举着蜜雪冰城饮料。他表情轻松,与...
力箭系列率先实现民商火箭“百星... 来源:滚动播报 (来源:千龙网) 昨天(15日)12时33分,中科宇航力箭一号遥十三运载火箭在东风商...
B站独家直播中科院公众科学日:... 央广网北京5月16日消息(记者 殷雨婷)5月16日至17日,中国科学院第22届公众科学日在全国一百余...
飞沃科技获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示飞沃科技(301232)新获得一项实用新型专利授权,专利名为“...
段永平的“本分”哲学,被OPP... 短短几十字的母亲节宣传文案,把OPPO推上了风口浪尖。 5月8日,手机品牌OPPO的官方微博、小红书...