MySQL之数据库管理语言(DML)

MySQL之数据库管理语言(DML)

DML是对表中的数据进行增、删、改的操作。

  1. 插入记录

    insert  into table_name  (列名1,列名2,...) values (列值1,列值2,...)
    

    列值和列名的类型、个数、顺序要一一对应
    列值不要超过列定义的长度
    如果插入空值,用null
    插入字符类型和日期类型要用单引号括起来

    例子:
    insert into user (id,name,birthday,salary) values(1,‘Tom’,‘2015-09-24’,1000);
    insert into user (id,name,birthday,salary) values(1,‘Tom’,‘2015-09-24’,205-7); //可以使用表达式
    insert into users values(default,‘Tom’,‘123’,3
    7-5,1); //为了主键自增,可以使用default或null
    insert into users values(default,‘Tom’,‘123’,default,1); //为字段填上默认值

  2. 修改操作(update)

    update table_name set 列名1 = 列值1,列名2 = 列值2,...[where where_condition]
    

    例子:
    update user set age = age+5; //将表中所有记录的age加5
    update user set age = age-id,sex = 0; //如果不加where,将更新表中所有记录
    update user set age = age+10 where id%2=0; //偶数记录年龄加10

  3. 删除操作 delete

    delete from table_name [where 列名=值]
    

    例子:
    delete from provinces where id=3; //删除表中id=3的记录
    delete from provinces; //删除表中i所有记录

猜你喜欢

转载自blog.csdn.net/qq_36548106/article/details/86294668