当前位置:   article > 正文

oracle常用的sql语句_oracle建表语句sql

oracle建表语句sql

1、创建表以及添加表注释和字段注释

  1. create table student(
  2. student_number varchar2(10),
  3. student_name varchar2(10),
  4. sex varchar2(4),
  5. score number(4)
  6. );
  7. comment on table student is '学生信息表';
  8. comment on column student.student_number is '学生学号';
  9. comment on column student.student_name is '学生姓名';
  10. comment on column student.sex is '性别';

2、ALTER TABLE

  1. //添加俩个字段grade和class
  2. alter table student add (grade varchar2(10),class varchar2(10));
  3. //删除grade
  4. alter table student drop column grade;
  5. //修改字段名或表名
  6. alter table student rename to student_new;
  7. alter table student rename column class to class_new;
  8. //修改字段类型和大小
  9. alter table student modify(class number(10));
  10. //增加删除启用禁用约束
  11. alter table student add constraint 约束名称 主键或外键等(字段)
  12. alter table student drop constraint 约束名称
  13. alter table student enable constraint 约束名称
  14. alter table student disable constraint 约束名称

3、UPDATE TABLE

  1. //把student的name字段的值全部设为张一山
  2. update student set name = '张一山'
  3. //找到student_number = '111000'对应的name,把name为张一山
  4. update student set student_name = '张一山' where student_number = '111000'
  5. //update语句的set子句支持同时将多个列作为更新对象。使用逗号将列进行分隔排列,这一方法
  6. 在所有的DBMS 中都可以使用。
  7. update student set score = score + 10,sex = '男'
  8. //同一张表,一个字段的数据更新到另一个字段(注意字段类型要相同)
  9. update student set student_name = student_number
  10. //不同表,一张表的一个字段更新到另一张表的一个字段
  11. /*
  12. 假设有另一张表student_subject的name字段数据是正确的,而student的name字段数据存在错误,现在
  13. 我们就需要把正确的数据更新到(或者说覆盖到)错误的数据中,where条件保证了学号相等的时候name字段才会覆盖。
  14. */
  15. update student set name = (select name from student_subject where student.学号 = student_subject.学号)
  16. //

INSERT语句

  1. //注意下面俩种插入方式的不同,可以选择字段插入
  2. insert into student values('111000','张三','男','68')
  3. insert into student(student_number,student_name) values('111000','张三')
  4. //同一个表,一个字段插入到另一个字段
  5. /*
  6. 这里是把学号的数据插入到姓名中,用update也可以做到,这种情况下用insert要注意被插入的字段没有数据才行
  7. */
  8. insert into student(student_name) select 学号 from student
  9. //不同表一个字段插入到另一个字段
  10. 如果表结构相同:insert into1 select * from2
  11. 如果表结构不同:insert into1(字段1,字段2,.....) select 字段1,字段2,..... from2
  12. //

DELETE语句与DROP语句

  1. //删除表中所有记录,清空数据
  2. delete from 表名
  3. //删除表中某一行
  4. delete from 表名 where 条件
  5. //删除整个表
  6. drop table 表名
  7. //删除整个用户
  8. /*
  9. cascade表示级联,意思是删除用户的同时,删除用户下的所有数据对象,如表....
  10. */
  11. drop user 用户名 cascade

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/519613
推荐阅读
相关标签
  

闽ICP备14008679号