七、sql基础:1.DML语句--【Oracle数据库】

七、DML语句

Data Manipulation Language,数据操纵语言,增删改。

1. INSERT语句
1.1 格式一

insert into student (stu_id, stu_name) values(12, ‘zhao’);

1.2 格式二

insert into student values (13, ‘xudong’ , null, null);

1.3 格式三:从另一个表中Copy一行
insert into sales_reps (id, name, salary, commission_pot)
        select employee_id, last_name, salary, commission_pot
        from  employees
        where   job_id  like  '%REP%';
1.4 格式四:使用子查询作为插入目标
    insert  into  
        (select  stu_id, stu_name, job_id  from employees where stu_id = 100)
    values (100, 'zhao', 12);

可以加上with check option来校验

    insert  into  
        (select  stu_id, stu_name, job_id  from employees where stu_id = 100 with check option)
    values (101, 'zhao', 12);

这样插入的时候就会报错,因为前面id=100,插入的是101


2. UPDATE语句
2.1格式一:更新某些符合条件的行中的某些列为具体值
    update  student  set  stu_name = 'zhao', stu_sex = 2
        where stu_id = 1 ;
2.2格式二:使用子查询的结果作为更新后的值
update  student  
    set  class_name = (select class_name from class where class_id = 11)
      class_num = (select class_num from class where class_id = 11)
    where  stu_id = 11;

3. DELETE语句
3.1格式一:删除某些符合条件的记录
delect  from  student 
    where stu_id = 10;
3.2格式二:删除一张表中所有的记录
delect from student;
truncate table student;语句也可以对表进行清空,效率更快,但是不能回滚。

4. MERGE语句:比较、整合

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_29668759/article/details/79965666
今日推荐