oracle表的创建、表的修改、数据操作

oracle表的创建、表的修改、数据操作

1、表的创建,字段数据类型自行设计,并设计相应约束
1)学生信息表,包含字段:学号、姓名、年龄、性别
2) 教师表,包含字段:教师编号、教师名字
3) 科目表,包含字段:课程编号、课程名字、教师编号
4) 成绩表,包含字段:学号、课程编号、成绩

2、表的修改
1) 修改学生信息表名为其他名字
2) 在学生信息表添加地址字段
3) 将地址字段的数据类型修改成其他数据类型

3、数据操作
1)学生信息表新增语句
2)学生信息表更新语句
3)学生信息表删除语句

--create the user 
--用户名
create user student11
--密码
identified by "123456";
--授予权限
grant connect,resource to student11;
-- revoke取消权限
--查询当前用户
select user from dual;

--创建学生表student
create table student(
    sno  varchar2(20) primary key,--学号
    sname varchar2(20) not null,--姓名
    ssex varchar2(20) not null,--性别
    sage number(5) not null--年龄
)

select *from student11.student;

--创建教师表teacher
create table teacher(
    tno varchar2(20) primary key,--教师编号
    tname  varchar2(20) not null--教师姓名
)

select *from student11.teacher;

--创建科目表subject
create table subject(
    sjno varchar2(20) primary key,--课程编号
    sjname  varchar2(20) not null,--课程名字
     tno  varchar2(20) not null --教师   
)
--创建成绩表score
create table score (
      sno  varchar2(20) not null,--学号 
      sjno varchar2(20) not null ,--课程编号
      scscore number(10,2) not null ,--成绩
      constraint pk_sc primary key(sno, sjno)--主键约束
)
--修改表名
alter table student rename to student01;
--查询 student01
select *from student11.student01;
--在学生信息表(student01)添加字段
ALTER TABLE student01 ADD sdep varchar(100);--院系
--查询 修改后的student01
select *from student11.student01;
--将 添加的字段改为varchar2类型
ALTER TABLE student01 MODIFY sdep varchar2(100);
--学生信息表新增信息(插入数据记录)
insert into student01(sno,sname,ssex,sage,sdep)
values('1','zi_chuan','男',22,'大数据学院');
--查询结果 
select * from student01 st01 where st01.sno='20170505210';
--更新学生表信息
update student01 st01
             set st01.sname='manxinlong',st01.sdep='信息工程学院';
             
--查询更新之后的结果 
select * from student01 st01 where st01.sno='20170505210';
--在删除前增加一条学生表的数据
insert into student01(sno,sname,ssex,sage,sdep)
values('20180505222','张三','男',22,'土木学院');
--查询张三
select * from student01 st01 where st01.sno='20170505211';
--删除张三
delete from student01 st01 where st01.sname='张三';
commit;
--删除后再次查询删除的张三数据为空
select * from student01 st01 where st01.sno='20180505222';

猜你喜欢

转载自blog.csdn.net/LL__Sunny/article/details/108782940