MySQL语言分类——DML

2022-10-17,,,

dml

dml的全称是database management language,数据库管理语言。主要包括以下操作:

insert、delete、update、optimize。

本篇对其逐一介绍

insert

数据库表插入数据的方式:

  1、insert的完整语法:(做项目的过程中将字段名全写上,这样比较容易看懂)

      单条记录插入语法:insert into table_name (column_name1,......) values (column_value1,......);

      多条记录插入语法:insert into table_name (column_name1,......) values (column_value1,......), (column_value1,......), (column_value1,......);

create table stu(
    id int primary key auto_increment,
    age int unsigned,
    name varchar(20),
    gender enum('0','1') default '0'
);
insert into stu(name,age) values('ls',17);
insert into stu(id,age,name,gender) values(1,18,'zs','1');
insert into stu(age,gender,name) values(15,'1','ww'),(17,'0','zl'),(20,'0','tq');

 

  2、windows终端命令行cmd导入:

      先切换到sql文件夹目录下:source xxxx.sql

  3、在windows终端命令行命令行模式中导出数据库、数据库表格

      导出数据库全部表格:mysqldump -h localhost -u root -p [-d] database_name > database_name.sql

        [-d] 表示不导出数据,建议不写,若不导出数据,没什么意义

      导出数据库的摸一个表格:mysqldump -h localhost -u root -p [-d] [--skip-lock-tables] database_name table_name > table_name.sql

        [--skip-lock-tables] 表示如果导出的表格与其他表格有关联,使用这个选项,就会排除这种顾虑

delete

  drop、truncate、delete三者的区别:

    drop一般用法是drop table table_name;它是删除表中的数据和结构,立刻释放磁盘空间

    truncate一般用法是truncate table table_name; 他是清空表中所有的数据,立刻释放磁盘空间

    delete一般用法是delete from table_name where conditions; 它是按照where之后的条件进行删除,不会释放磁盘空间;如果没有条件,就清空表中数据。

  对于数据而言:前两者,一旦删除就找不回来,而delete删除的时候没有提交事务,可以通过回滚事务复原删除的数据,如果提交了,也是一样找不回来的

  delete的完整语法:

    delete [low_priority] from table_name [where conditions] [limit n]

      [low_priority] 表示如果没有客户读取当前表格,才执行delete语句

      [limit n] 表示删除的记录数量

谨慎操作:删除数据之前先进行select:
select * from stu where id>7 and gender='0';
delete from stu where id>7 and gender='0';

seelct * from stu where name is null;
delete from stu where name is null limit 2;

update    

delete的完整语法:

    update [low_priority] table_name set column_name=column_value,.... [where conditions] [limit n]

      [low_priority] 表示如果没有客户读取当前表格,才执行delete语句

      [limit n] 表示删除的记录数量

谨慎操作:修改数据之前先select:
select * from stu where id=12;
update stu set name='zs',gender='1' where id=12;

optimize

  语法:optimize table tbl_name

  你可以使用optimize table回收闲置的空间。

optimize table通过制作原来的表的一个临时副本来工作。老的表被拷贝到 新表中(没有闲置的行),然后原来的表被删除并且重命名一个新的。这样做使得 所有更新自动转向新的表,没有任何失败的更新。当时optimize table正在 执行时,原来的表可被另外的客户读取。对表的更新和写入延迟到新表是准备好为止。

 

《MySQL语言分类——DML.doc》

下载本文的Word格式文档,以方便收藏与打印。