首页 > 数据库 > mysql教程 > MySQL之终端Terminal(dos界面)管理数据库、数据表、数据的基本操作_MySQL

MySQL之终端Terminal(dos界面)管理数据库、数据表、数据的基本操作_MySQL

WBOY
发布: 2016-06-01 13:04:43
原创
815 人浏览过

MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”。 现在我写MySQL的终端命令操作的文章,是想强化一下自己对于MySQL的理解,总会比使用图形化的理解透彻,因为我本来就比较喜欢写代码。同时写出来这些文章,是想要给大家当个参考,希望也能对大家有所帮助,有所提升,这就是我为什么要写终端操作MySQL的文章了。

 

注意:MySQL数据库命令不区分大小写。但在MAC的终端,如果你想使用tab自动补全命令,那么你就必须使用大写,这样MAC的终端才会帮你补全命令,否则你按N遍tab都不会有响应。

 

1、数据库(database)管理

1.1 create 创建数据库

1.2 show 查看所有数据库

1.3 alter 修改数据库

1.4 use 使用数据库

1.5 查看当前使用的数据库

1.6 drop 删除数据库

2、数据表(table)管理

2.1 create 创建表

2.2 show 显示表

2.3 desc 查看表结构

2.4 alter 修改表结构(增、删、改)

2.4.1 insert 在表中添加列(字段)

2.4.2 alter 修改表(列)字段

2.4.3 delete 删除表(列)字段

2.4.4 rename 重命名表名

2.5 create 利用已有数据创建新表

3、数据的操作及管理

3.1 增加数据(增)

3.2 删除数据(删)

3.3 修改数据(改)

3.4 查询数据(查)

 

 

 

1、数据库(database)管理

1.1 create 创建数据库

<span style="color: #0000ff">create</span> <span style="color: #0000ff">database</span> firstDB;
登录后复制

 

1.2 show 查看所有数据库

mysql<span style="color: #808080">></span><span style="color: #000000"> show databases;
 
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------------------+</span>
<span style="color: #808080">|</span> <span style="color: #0000ff">Database</span>      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------------------+</span>
<span style="color: #808080">|</span> information_schema <span style="color: #808080">|</span>
<span style="color: #808080">|</span> firstDB      <span style="color: #808080">|</span>
<span style="color: #808080">|</span> mysql       <span style="color: #808080">|</span>
<span style="color: #808080">|</span> performance_schema <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

1.3 alter 修改数据库

alter 命令修改数据库编码:

默认创建的数据库默认不支持中文字符,如果我们需要它支持中文字符,则将它的编码设置为utf8格式:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">ALTER</span> <span style="color: #0000ff">DATABASE</span> testDB <span style="color: #0000ff">CHARACTER</span> <span style="color: #0000ff">SET</span><span style="color: #000000"> UTF8;
Query OK, </span><span style="color: #800000; font-weight: bold">1</span> row affected (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

1.4 use 使用数据库

mysql<span style="color: #808080">></span> <span style="color: #0000ff">use</span><span style="color: #000000"> firstDB;
</span><span style="color: #0000ff">Database</span> changed
登录后复制

 

1.5 查看当前使用的数据库

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #0000ff">database</span><span style="color: #000000">();
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------+</span>
<span style="color: #808080">|</span> <span style="color: #0000ff">database</span>() <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------+</span>
<span style="color: #808080">|</span> firstdb  <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------+</span>
<span style="color: #800000; font-weight: bold">1</span> row <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

 

1.6 drop 删除数据库

mysql<span style="color: #808080">></span> <span style="color: #0000ff">drop</span> <span style="color: #0000ff">database</span><span style="color: #000000"> firstDB;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

 

 

2、数据表(table)管理

我们首先创建一个数据库,提供我们往后的使用:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">create</span> <span style="color: #0000ff">database</span><span style="color: #000000"> testDB;
Query OK, </span><span style="color: #800000; font-weight: bold">1</span> row affected (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

创建后记得用use命令进入(使用)数据库,不然后面的操作都会不成功的。

 

2.1 create 创建表

mysql<span style="color: #808080">></span> <span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span><span style="color: #000000"> PEOPLE (
  </span><span style="color: #808080">-></span> ID <span style="color: #0000ff">int</span> AUTO_INCREMENT <span style="color: #0000ff">PRIMARY</span> <span style="color: #0000ff">KEY</span><span style="color: #000000">,
  </span><span style="color: #808080">-></span> NAME <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>) <span style="color: #808080">not</span> <span style="color: #0000ff">null</span><span style="color: #000000">,
  </span><span style="color: #808080">-></span> AGE <span style="color: #0000ff">int</span> <span style="color: #808080">not</span> <span style="color: #0000ff">null</span><span style="color: #000000">,
  </span><span style="color: #808080">-></span> BIRTHDAY <span style="color: #0000ff">datetime</span><span style="color: #000000">);                           
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.01</span> sec)
登录后复制

 

2.2 show 显示表

显示当前数据库所有的数据表

mysql<span style="color: #808080">></span><span style="color: #000000"> show tables;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #808080">|</span> Tables_in_testdb <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #808080">|</span> PEOPLE      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #800000; font-weight: bold">1</span> row <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

2.3 desc 查看表结构

mysql<span style="color: #808080">></span> <span style="color: #0000ff">desc</span><span style="color: #000000"> PEOPLE
  </span><span style="color: #808080">-></span><span style="color: #000000"> ;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> Field  <span style="color: #808080">|</span> Type    <span style="color: #808080">|</span> <span style="color: #0000ff">Null</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Key</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Default</span> <span style="color: #808080">|</span> Extra     <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> ID    <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span> PRI <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span> auto_increment <span style="color: #808080">|</span>
<span style="color: #808080">|</span> NAME   <span style="color: #808080">|</span> <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>) <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> AGE   <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> BIRTHDAY <span style="color: #808080">|</span> <span style="color: #0000ff">datetime</span>  <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.01</span> sec)
登录后复制

 

2.4 alter 修改表结构(增、删、改)

默认创建的表不支持中文字符,所以需将表编码设置为utf8:

mysql><span style="color: #000000"> ALTER TABLE KEYCHAIN CONVERT TO CHARACTER SET UTF8;
Query OK, </span><span style="color: #800080">1</span> row affected (<span style="color: #800080">0.02</span><span style="color: #000000"> sec)
Records: </span><span style="color: #800080">1</span> Duplicates: <span style="color: #800080">0</span> Warnings: <span style="color: #800080">0</span>
登录后复制

 

 

2.4.1 insert 在表中添加列(字段)

mysql<span style="color: #808080">></span> <span style="color: #0000ff">alter</span> <span style="color: #0000ff">table</span> PEOPLE <span style="color: #0000ff">add</span><span style="color: #000000"> star BOOL;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.02</span><span style="color: #000000"> sec)
Records: </span><span style="color: #800000; font-weight: bold">0</span> Duplicates: <span style="color: #800000; font-weight: bold">0</span> Warnings: <span style="color: #800000; font-weight: bold">0</span>
登录后复制

提示:在MySQL里,布尔类型会自动转换为tinyint(1)类型。

 

我们不妨使用desc去查看一下PEOPLE表结构:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">desc</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> Field  <span style="color: #808080">|</span> Type    <span style="color: #808080">|</span> <span style="color: #0000ff">Null</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Key</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Default</span> <span style="color: #808080">|</span> Extra     <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> ID    <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span> PRI <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span> auto_increment <span style="color: #808080">|</span>
<span style="color: #808080">|</span> NAME   <span style="color: #808080">|</span> <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>) <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> AGE   <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> BIRTHDAY <span style="color: #808080">|</span> <span style="color: #0000ff">datetime</span>  <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> star   <span style="color: #808080">|</span> <span style="color: #0000ff">tinyint</span>(<span style="color: #800000; font-weight: bold">1</span>) <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #800000; font-weight: bold">5</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

现在,你该相信我了吧?

 

2.4.2 alter 修改表(列)字段

mysql<span style="color: #808080">></span> <span style="color: #0000ff">alter</span> <span style="color: #0000ff">table</span> PEOPLE MODIFY star <span style="color: #0000ff">int</span><span style="color: #000000">;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.01</span><span style="color: #000000"> sec)
Records: </span><span style="color: #800000; font-weight: bold">0</span> Duplicates: <span style="color: #800000; font-weight: bold">0</span> Warnings: <span style="color: #800000; font-weight: bold">0</span>
登录后复制

 

我们再次使用desc查看PEOPLE表结构:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">desc</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> Field  <span style="color: #808080">|</span> Type    <span style="color: #808080">|</span> <span style="color: #0000ff">Null</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Key</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Default</span> <span style="color: #808080">|</span> Extra     <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> ID    <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span> PRI <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span> auto_increment <span style="color: #808080">|</span>
<span style="color: #808080">|</span> NAME   <span style="color: #808080">|</span> <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>) <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> AGE   <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> BIRTHDAY <span style="color: #808080">|</span> <span style="color: #0000ff">datetime</span>  <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> star   <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #800000; font-weight: bold">5</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

2.4.3 delete 删除表(列)字段

mysql<span style="color: #808080">></span> <span style="color: #0000ff">alter</span> <span style="color: #0000ff">table</span> PEOPLE <span style="color: #0000ff">DROP</span> <span style="color: #0000ff">column</span><span style="color: #000000"> star;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.02</span><span style="color: #000000"> sec)
Records: </span><span style="color: #800000; font-weight: bold">0</span> Duplicates: <span style="color: #800000; font-weight: bold">0</span> Warnings: <span style="color: #800000; font-weight: bold">0</span>
登录后复制

 

删除后,再次查看PEOPLE表结构:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">desc</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> Field  <span style="color: #808080">|</span> Type    <span style="color: #808080">|</span> <span style="color: #0000ff">Null</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Key</span> <span style="color: #808080">|</span> <span style="color: #0000ff">Default</span> <span style="color: #808080">|</span> Extra     <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #808080">|</span> ID    <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span> PRI <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span> auto_increment <span style="color: #808080">|</span>
<span style="color: #808080">|</span> NAME   <span style="color: #808080">|</span> <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>) <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> AGE   <span style="color: #808080">|</span> <span style="color: #0000ff">int</span>(<span style="color: #800000; font-weight: bold">11</span>)   <span style="color: #808080">|</span> NO  <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">|</span> BIRTHDAY <span style="color: #808080">|</span> <span style="color: #0000ff">datetime</span>  <span style="color: #808080">|</span> YES <span style="color: #808080">|</span>   <span style="color: #808080">|</span> <span style="color: #0000ff">NULL</span>  <span style="color: #808080">|</span>        <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--------+-------------+------+-----+---------+----------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

删除字段成功,现在我们已经不能看到star的字段了。

 

2.4.4 rename 重命名表名

mysql<span style="color: #808080">></span> RENAME <span style="color: #0000ff">TABLE</span> PEOPLE <span style="color: #0000ff">TO</span><span style="color: #000000"> NEW_PEOPLE;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

 

2.5 create 利用已有数据创建新表

mysql<span style="color: #808080">></span> <span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span> newTable <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
Query OK, </span><span style="color: #800000; font-weight: bold">0</span> rows affected (<span style="color: #800000; font-weight: bold">0.01</span><span style="color: #000000"> sec)
Records: </span><span style="color: #800000; font-weight: bold">0</span> Duplicates: <span style="color: #800000; font-weight: bold">0</span> Warnings: <span style="color: #800000; font-weight: bold">0</span>
登录后复制

 

我们查看一下目前数据库存在的表:

mysql<span style="color: #808080">></span><span style="color: #000000"> show tables;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #808080">|</span> Tables_in_testdb <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #808080">|</span> PEOPLE      <span style="color: #808080">|</span>
<span style="color: #808080">|</span> newTable     <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">----------------+</span>
<span style="color: #800000; font-weight: bold">2</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

 

 

3、数据的操作及管理

数据表的基本操作,包含增、删、改、查数据。

 

以下命令均在PEOPLE表上操作。

 

3.1 增加数据(增)

PEOPLE表目前是没有数据的,它是空的数据表,我们现在先添加一些数据。

insert into 命令添加数据:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">insert</span> <span style="color: #0000ff">into</span> PEOPLE <span style="color: #0000ff">VALUES</span> (<span style="color: #0000ff">null</span>, <span style="color: #ff0000">‘</span><span style="color: #ff0000">Anny</span><span style="color: #ff0000">‘</span>, <span style="color: #800000; font-weight: bold">22</span>, <span style="color: #ff0000">‘</span><span style="color: #ff0000">1992-05-22</span><span style="color: #ff0000">‘</span><span style="color: #000000">);
Query OK, </span><span style="color: #800000; font-weight: bold">1</span> row affected (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

使用select命令查看表(会在后面介绍),现在我们查看PEOPLE数据表的数据:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+------+-----+---------------------+</span>
<span style="color: #808080">|</span> ID <span style="color: #808080">|</span> NAME <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+------+-----+---------------------+</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1</span> <span style="color: #808080">|</span> Anny <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">1</span> row <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

数据表现在有一条数据。

 

我们多添加几条数据,如:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> ID <span style="color: #808080">|</span> NAME  <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1</span> <span style="color: #808080">|</span> Anny  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">2</span> <span style="color: #808080">|</span> Garvey <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">23</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">3</span> <span style="color: #808080">|</span> Lisa  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">25</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1989</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">4</span> <span style="color: #808080">|</span> Nick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1990</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">5</span> <span style="color: #808080">|</span> Rick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">5</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

 

3.2 删除数据(删)

delete 命令删除数据:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">delete</span> <span style="color: #0000ff">from</span> PEOPLE <span style="color: #0000ff">where</span> name <span style="color: #808080">=</span> <span style="color: #ff0000">‘</span><span style="color: #ff0000">Lisa</span><span style="color: #ff0000">‘</span><span style="color: #000000">;
Query OK, </span><span style="color: #800000; font-weight: bold">1</span> row affected (<span style="color: #800000; font-weight: bold">0.01</span> sec)
登录后复制

 

再次查询PEOPLE表:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> ID <span style="color: #808080">|</span> NAME  <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1</span> <span style="color: #808080">|</span> Anny  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">2</span> <span style="color: #808080">|</span> Garvey <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">23</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">4</span> <span style="color: #808080">|</span> Nick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1990</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">5</span> <span style="color: #808080">|</span> Rick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

已经看不到名为“Lisa”的数据了。

 

3.3 修改数据(改)

update 命令修改数据:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">update</span> PEOPLE <span style="color: #0000ff">set</span> name<span style="color: #808080">=</span><span style="color: #ff0000">‘</span><span style="color: #ff0000">Calvin</span><span style="color: #ff0000">‘</span> <span style="color: #0000ff">where</span> name <span style="color: #808080">=</span> <span style="color: #ff0000">‘</span><span style="color: #ff0000">Garvey</span><span style="color: #ff0000">‘</span><span style="color: #000000">;
Query OK, </span><span style="color: #800000; font-weight: bold">1</span> row affected (<span style="color: #800000; font-weight: bold">0.00</span><span style="color: #000000"> sec)
Rows matched: </span><span style="color: #800000; font-weight: bold">1</span> Changed: <span style="color: #800000; font-weight: bold">1</span> Warnings: <span style="color: #800000; font-weight: bold">0</span>
登录后复制

 

查询PEOPLE表内容:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> ID <span style="color: #808080">|</span> NAME  <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1</span> <span style="color: #808080">|</span> Anny  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">2</span> <span style="color: #808080">|</span> Calvin <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">23</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">4</span> <span style="color: #808080">|</span> Nick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1990</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">5</span> <span style="color: #808080">|</span> Rick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制
登录后复制

名为“Garvey”的记录已经修改为“Calvin”。

 

3.4 查询数据(查)

select 命令查询数据,最简单的就是查询表的所有数据,也就是我们最初使用到的那条命令:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> <span style="color: #808080">*</span> <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> ID <span style="color: #808080">|</span> NAME  <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1</span> <span style="color: #808080">|</span> Anny  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">2</span> <span style="color: #808080">|</span> Calvin <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">23</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">4</span> <span style="color: #808080">|</span> Nick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1990</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">5</span> <span style="color: #808080">|</span> Rick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">--+--------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制
登录后复制

格式:select * from ,*代表所有字段。 

 

查询数据时也可指定显示的(列)字段:

mysql<span style="color: #808080">></span> <span style="color: #0000ff">select</span> NAME, AGE, BIRTHDAY <span style="color: #0000ff">from</span><span style="color: #000000"> PEOPLE;
</span><span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------+-----+---------------------+</span>
<span style="color: #808080">|</span> NAME  <span style="color: #808080">|</span> AGE <span style="color: #808080">|</span> BIRTHDAY      <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------+-----+---------------------+</span>
<span style="color: #808080">|</span> Anny  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">22</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1992</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> Calvin <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">23</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> Nick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1990</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">|</span> Rick  <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">24</span> <span style="color: #808080">|</span> <span style="color: #800000; font-weight: bold">1991</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">05</span><span style="color: #808080">-</span><span style="color: #800000; font-weight: bold">22</span> <span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span>:<span style="color: #800000; font-weight: bold">00</span> <span style="color: #808080">|</span>
<span style="color: #808080">+</span><span style="color: #008080">--</span><span style="color: #008080">------+-----+---------------------+</span>
<span style="color: #800000; font-weight: bold">4</span> rows <span style="color: #808080">in</span> <span style="color: #0000ff">set</span> (<span style="color: #800000; font-weight: bold">0.00</span> sec)
登录后复制

格式:select from 。

 

select查询命令还有很多的高级用法,比如用来查找不重复(distinct)的数据,使数据按条件排序(order by),按查询条件显示数据(where)等等。这些都会在下一篇文章作重点介绍,请大家继续留意我的博客,谢谢。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板