DML_数据操纵语言

DML语法:
        insert  注意点:1.在表后可以有括号,表明  所插入的值是哪几列,但是一定要包括所有的not null属性
                                            2.当要为一个表插入的一行数据中有FK,先看FK的值是否存在,
                                               若不存在,则先添加外键所在表中的值,之后再插入
  1. /*
  2. * insert 添加数据
  3. * insert into tableName values('所有数据')
  4. * insert into tableName(部分列名 - 必须包含所有非空列) values('部分列数据')
  5. */
  6. insert into course values('C01', 'JavaSE', 100);
  7. insert into course values('C02', 'JavaSE', '');
  8. insert into course(courseno, coursename) values('C03', 'Oracle');
  9. --insert into course(courseno) values('C04'); -- wrong
  10. -- char固定长度
  11. insert into school values('S8372222', '中国大学');
  12. insert into school values('S8372223', '中国三美大学');
  13. insert into faculty values('01', '计算机系', 'S8372223');
  14. insert into faculty values('02', '土木工程系', 'S8372223');
  15. insert into faculty values('03', '计算机系', 'S8372222');
  16. insert into faculty values('04', '土木工程系', 'S8372222');
  17. insert into major values('M0001', '软件工程', '01');
  18. insert into major values('M0002', '网络工程', '01');
  19. insert into major values('M0003', '建筑', '02');
  20. insert into major values('M0004', '建筑美学', '02');
  21. insert into major values('M0005', '软件工程', '03');
  22. insert into major values('M0006', '网络工程', '03');
  23. insert into major values('M0007', '建筑', '04');
  24. insert into major values('M0008', '建筑美学', '04');
  25. insert into studentcard values('S87232', '中国大学', '张思思', '11级软件04班');
  26. -- insert添加 FK引入PK的值 有值直接使用 无值先insert数据再插入
  27. insert into student values('S0001', '张思思', '', null, null, '无锡', '', 'M0008', 'S87232');
  28. insert into studentcard values('S87233', '中国大学', '张珊珊', '11级软件04班');
  29. insert into student values('S0002', '张珊珊', '女', 20, null, '北京', '68', 'M0005', 'S87233');

update 注意点:update tableName set 列名 = value, ... where ...

  1. update tableName set 列名 = value, ... where ...
  2. -- 部分数据更新 where条件
  3. update student set age = 20;
  4. -- 某一条 PK UK
  5. select * from student; -- PK UK
  6. update student set name = '张三散', sex = '男' where stuNo = 'S9999';
  7. select * from course;
  8. update course set hour = 200 where courseno = 'C05';
  9. 用户登录
  10. update users set password = '123456' where userName = 'admin';
  11. update users set info = 'newInfo' where userName = 'admin';
  12. 中国大学 - 中国社会大学
  13. update school set schoolName = '中国社会大学' where schoolname = '中国大学';
  14. -- 某几条数据 FK
  15. -- 教学部 所有员工 奖金+200
  16. update employee set bonus = bonus + 200
  17. where deptNo = (select deptNo from dept where deptName = '教学部');
  18. -- 软件工程 专业学生 总+10
  19. update student set score = score + 10
  20. where majorno in (select majorNo from major where name = '软件工程');
  21. -- 计算机部 学生总分 + 5
  22. update student set score = score + 5
  23. where unionno = (select unionno from studentunion where unionname = '计算机部');
  24. -- 所有员工工资 + 500
  25. update employee set salary = salary + 500;

    delete    注意点:1.当只想删除一条记录时,where后的条件属性必须是FK  或者  UK
                               2.当一张表中有FK时,有两种方案可以删除(按具体情况使用):
                                                 a. 同时删除(例:删帖子,那么帖子下的回复内容自然同时删除了)
                                                 b.先更新再删除

  1. delete from tableName where ...
  2. -- 删除所有数据
  3. delete from course;
  4. -- 删除某一条数据 PK UK
  5. select * from course;
  6. delete from course where courseno = 'C05';
  7. 某员工辞职
  8. delete from employee where empNo = 'empNo';
  9. select * from student;
  10. delete from student where stuNo = 'S9008';
  11. 某商品下架
  12. -- 中国大学 计算机系 撤销
  13. -- 删除数据 数据FK被引用
  14. --学生
  15. update student set majorno = 'M0007'
  16. where majorno in (select majorno from major
  17. where facultyno =(select facultyno from faculty
  18. where fname = '计算机系' and schoolcode
  19. = (select schoolcode from school
  20. where schoolname = '中国大学')));
  21. --专业
  22. delete from major where facultyno = (select facultyno from faculty
  23. where fname = '计算机系' and schoolcode
  24. = (select schoolcode from school
  25. where schoolname = '中国大学'));
  26. --院系
  27. delete from faculty where fname = '计算机系'
  28. and schoolcode = (select schoolcode from school
  29. where schoolname = '中国大学');
  30. -- 同时删除
  31. 课程C01 JavaSE 100 开设有问题
  32. delete from studentcourse where courseno = 'C01';
  33. delete from course where courseno = 'C01';
  34. BBS论坛
  35. 帖子 - 回复 帖子内容有严重问题
  36. delete from reply where postId = '0001';
  37. delete from post where postId = '0001';
  38. -- 更新 再删除
  39. -- 大学毕业 学生证失效
  40. update student set cardno = null;
  41. delete from studentcard;






























posted @ 2016-09-21 15:47  无丑不成戏如人生  阅读(409)  评论(0编辑  收藏  举报