zoukankan      html  css  js  c++  java
  • Dos命令操作数据库。

    1.查看数据库的指令 show databases;

    ;”分号是数据库的结束符,没有加分号 即使按回车 也代表这个语句没有结束

      

      如下:mysql> show databases;

      出现结果:

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

        | Database           |

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

        | information_schema |

        | d1                 |

        | mysql              |

        | performance_schema |

        | test               |

        | y2                 |

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

        6 rows in set (0.00 sec)

    2. 创建数据库 create database 数据库名;

    如下:mysql> create database y1;

    出现结果:

      Query OK, 1 row affected (0.00 sec)

    (进行查看是否出现:)

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

    | Database           |

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

    | information_schema |

    | mysql              |

    | performance_schema |

    | test               |

    | y1                 |

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

    3.删除数据库的指令  drop database 数据库名;

    如下:mysql> drop database y2;

    出现结果:

    Query OK, 0 rows affected (0.00 sec)

    (进行查看是结果:)

    mysql> show databases;

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

    | Database           |

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

    | information_schema |

    | mysql              |

    | performance_schema |

    | test               |

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

    4 rows in set (0.00 sec)

    2. 进入某一个数据库 use 数据库名;

    如下:mysql> use d1;

    出现结果:

    Database changed

    5.对数据表增删查改

    查看数据表 show tables;

    如下:mysql> create table t2(id int,name int);

    出现结果:Query OK, 0 rows affected (0.46 sec)

    4..创建表

    Create table 表名(字段1 类型,字段2 类型)

    如下:create table t1(id int,name int);

    出现结果:Query OK, 0 rows affected (0.46 sec)

    (进行查看结果:)

    mysql> show tables;

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

    | Tables_in_d1 |

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

    | t1           |

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

    1 row in set (0.00 sec)

    5.修改表名

    Alter table 表名 rename 新表名;

    如下:mysql> alter table t1 rename t3;

    出现结果:Query OK, 0 rows affected (0.01 sec)

    (进行查看结果:)

    mysql> show tables;

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

    | Tables_in_d1 |

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

    | t3           |

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

    2 rows in set (0.00 sec)

    6.进入表里对字段进行操作

    查看表的定义 desc 表名;

    如下:mysql> desc t2;

    出现结果:

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

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

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

    | id    | int(11) | YES  |     | NULL    |       |

    | name  | int(11) | YES  |     | NULL    |       |

    | age   | int(11) | YES  |     | NULL    |       |

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

    7.添加字段

    字段定义  字段名和字段类型 都要写

    alter table 表名 add 字段定义;

    如下:mysql> alter table t2 add age int;

    出现结果:

    Query OK, 0 rows affected (0.17 sec)

    Records: 0  Duplicates: 0  Warnings: 0

    8.删除字段

    Alter table 表名 drop 字段名;

    如下:mysql> alter table t2 add age int;

    出现结果:

    Query OK, 0 rows affected (0.26 sec)

    Records: 0  Duplicates: 0  Warnings: 0

    (进行查看结果:)

    mysql> desc t2;

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

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

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

    | id    | int(11) | YES  |     | NULL    |       |

    | name  | int(11) | YES  |     | NULL    |       |

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

    2 rows in set (0.01 sec)

    9.修改字段

    Alter table 表名 change 旧的字段名  字段定义;

    如下:mysql> alter table t2 change name age char;

    出现结果:

    Query OK, 0 rows affected (0.52 sec)

    Records: 0  Duplicates: 0  Warnings: 0

    进行查看结果:)

    mysql> desc t2;

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

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

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

    | id    | int(11) | YES  |     | NULL    |       |

    | age   | char(1) | YES  |     | NULL    |       |

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

    2 rows in set (0.01 sec)

    10.修改字段类型

    如下:mysql> alter table t2 modify age int;

    出现结果:

    Query OK, 0 rows affected (0.42 sec)

    Records: 0  Duplicates: 0  Warnings: 0

    (进行查看结果:)

    mysql> desc t2;

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

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

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

    | id    | int(11) | YES  |     | NULL    |       |

    | age   | int(11) | YES  |     | NULL    |       |

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

    2 rows in set (0.01 sec)

    11.给字段添加数据(记录)

    添加一条记录

    Insert into 表名(id,age) value(1,值2)

    如下:mysql> insert into t3(id,name) value(1,111);

    mysql> insert into t3(id,name) value(2,222);

    mysql> insert into t3(id,name) value(3,333);

    出现结果:Query OK, 1 row affected (0.00 sec)

    (进行查看结果:)

    mysql> select * from t3;

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

    | id   | name | age  |

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

    |    1 |  111 | NULL |

    |    2 |  222 | NULL |

    |    3 |  333 | NULL |

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

    3 rows in set (0.00 sec)

    12.多条记录添加

    Insert into 表名 values(1,值2),(1,值2),(1,值2),(1,值2);

    如下:mysql>insert into t5 values(1,20),(2,21),(3,26);

    出现结果:

    Query OK, 3 rows affected (0.00 sec)

    Records: 3  Duplicates: 0  Warnings: 0

    查看记录

    Select * from 表名; 查看所有的字段记录

    Select id from 表名; 查看单个的字段记录

    Select id,age from 表名;查看多个字段的记录

    mysql> select * from t4;

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

    | id   | age |

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

    |    1 |   20 |

    |    2 |   21 |

    |    3 |   26 |

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

    3 rows in set (0.00 sec)

    按条件查询

    Select * from 表名 where 条件

    条件表达式  > < >= <= = !=    and且  or

    如下:mysql> select * from t5 where age>30;

    mysql> select * from t5 where age>30 and age<50;

    出现结果:

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

    | id   | age  |

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

    |    4 |   56 |

    |    5 |   62 |

    |    8 |   54 |

    |    9 |   34 |

    |   10 |   36 |

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

    5 rows in set (0.00 sec)

    (出现结果:

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

    | id   | age  |

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

    |    9 |   34 |

    |   10 |   36 |

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

    2 rows in set (0.10 sec)

      

    排序查询

    Select * from 表名 order by 字段名 [asc/desc]

    Asc 由低到高  desc 由高到底

    如下:mysql> select * from t5 order by age desc;

    出现结果;

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

    | id   | age  |

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

    |    5 |   62 |

    |    4 |   56 |

    |    8 |   54 |

    |   10 |   36 |

    |    9 |   34 |

    |    3 |   26 |

    |    2 |   21 |

    |    1 |   20 |

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

    8 rows in set (0.10 sec)

     

    限制查询

    Select * from 表名 limit 2,5;从第二个开始向后查询五个

    如下:mysql> select * from t5 limit 2,5;

    出现结果;

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

    | id   | age  |

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

    |    3 |   26 |

    |    4 |   56 |

    |    5 |   62 |

    |    8 |   54 |

    |    9 |   34 |

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

    5 rows in set (0.00 sec)

    如下:mysql> select * from t5 limit 5;

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

    | id   | age  |

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

    |    1 |   20 |

    |    2 |   21 |

    |    3 |   26 |

    |    4 |   56 |

    |    5 |   62 |

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

    5 rows in set (0.00 sec)

    删除记录

    删除所有

    Delete from 表名

    按条件删  delete from 表名 where 条件表达式;

    如下:mysql> delete from t5 where id=4;

    出现结果:

    Query OK, 1 row affected (0.13 sec)

    查看输出结果)

    mysql> select * from t5;

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

    | id   | age  |

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

    |    1 |   20 |

    |    2 |   21 |

    |    3 |   26 |

    |    5 |   62 |

    |    8 |   54 |

    |    9 |   34 |

    |   10 |   36 |

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

    7 rows in set (0.00 sec)

    改数据

    Update 表名 set 字段=值;

    如果不带条件 会把字段下面的记录全改

    如下:mysql> select * from t5;

    出现结果:

    Query OK, 7 rows affected (0.10 sec)

    Rows matched: 7  Changed: 7  Warnings: 0

    (查看输出结果)

    mysql> select * from t5;

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

    | id   | age  |

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

    |    1 |   12 |

    |    2 |   12 |

    |    3 |   12 |

    |    5 |   12 |

    |    8 |   12 |

    |    9 |   12 |

    |   10 |   12 |

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

    7 rows in set (0.00 sec)

    按条件更新

    如下:mysql> update t5 set age=56 where id=6;

    出现结果:

    Query OK, 0 rows affected (0.00 sec)

    Rows matched: 0  Changed: 0  Warnings: 0

    (查看输出结果)

    mysql> select * from t5;

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

    | id   | age  |

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

    |    1 |   12 |

    |    2 |   12 |

    |    3 |   12 |

    |    5 |   12 |

    |    8 |   12 |

    |    9 |   12 |

    |   10 |   12 |

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

    7 rows in set (0.00 sec)

  • 相关阅读:
    clear session on close of browser jsp
    [SQL Server]从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。
    Windows Server 2012上安装.NET Framework 3.5
    SqlServer安装时的选项说明
    js 动态控制 input 框 的只读属性
    svn备份与还原_脚本_(dump命令)
    几种任务调度的 Java 实现方法与比较
    在WordPress正文顶端或者末尾插入固定的内容
    TODO
    fileUpload(草稿)
  • 原文地址:https://www.cnblogs.com/xyangjie/p/10686109.html
Copyright © 2011-2022 走看看