MySQL基本操作命令

    xiaoxiao2023-07-08  141

    (注意:MySQL操作语句已“;”为结束标志,一个语句段写完,必须打“;”如果忘记打在下一行打“;”回车。)

     

    1,create databases 数据库名称 ;

    如,创建名为linda的数据库,SQL语句如下:

    create databases linda;

     

    2,显示已经存在的数据库:

    Show databases;

     

    3,显示库中的数据表:

    use mysql; //选择你要显示表的数据库

    show tables;

     

    4,建表:

    use 库名;

    create table 表名 (字段设定列表);

    创建数据库表的基本语法格式:

    create table 表名

    {

        字段名 1,数据类型[完整性约束条件],

        字段名 2,数据类型[完整性约束条件],

       ...

        字段名 n,数据类型[完整性约束条件],

    }

     

    5,删库和删表:

    drop database 库名;

    drop table 表名;

     

    6,将表中记录清空:

    delete from 表名;

     

    7,显示表中的记录:

    select * from 表名;

     

    8,向表中添加数据:

    A:指定字段名添加:

    insert into 表名(字段名1,字段名2, ...) value(值1,值2, ...);

      B:不指定字段名添加:

    insert into 表名 values(值1,值2, ...);

     

    9,更新表中数据:

    update 表名

       set 字段名1 = 值1 [,字段名2 = 值2, ...]

    [where 条件表达式]

     

    10,删除表中数据:

    delete from 表名 [where 条件表达式]

     

    11,查询表中数据:

    select [distinct] *| {字段名1,字段名2,字段名3, ...}

    from 表名

    [where 条件表达式1]

    [group by 字段名 [having 条件表达式2]]

    [order by 字段名 [asc|desc]]

    [limit [offset] 记录数]

    (asc是升序排列,desc是降序排列)

     

    12,模糊查询:

    select *|{字段名1, 字段名2, ...}

       from 表名

    where 字段名 [not]like 'XXX' ;

    13,and 和or的使用:

    使用and时必须条件都满足,使用or时只需要满足一个就可以。

    例如:查询student表中gender字段值为“女”或者gender字段值为“男”,并且grade字段值为100的学生姓名.

    select name, grade, gender

     fromstudent

     wheregender = '女' or gender = '男' and grade = 100 ;

    (注意:and的优先级高于or)

     

    14,修复损坏的表      

    ①、用root帐号从命令行登录MySQL:        mysql -u root -p

      ②、输入root帐号的密码。

      ③、选定数据库名(本例中的数据库名为blog):  use blog;

    ④、修复损坏的表(本例中要修复的表为users):   repair table users;udent;

    15, 备份数据库

    mysqldump -uroot-p test_db > test_db.sql

    16,恢复数据库

    mysql -uroot -ptest_db < test_db.sql

    17,创建权限

    grant allprivileges on test_db.* to test_db@'localhost' identified by '123456';

    兼容mysql4.1之前模式:

    updatemysql.user set password=old_password('123456') where user='test_db';

    18,忘记密码

    在“my.cnf”或“my.ini”文件的“mysqld”配置段添加“skip-grant-tables”,然后重新启动mysql即可登录修改root密码。

    相关资源:java调用mysql命令 导入导出数据库
    最新回复(0)