2018.5.19 Oracle数据操作和管理表的综合练习

--作业一、使用自己的用户登录,完成如下操作,并且创建5条测试数据

-- 创建学生表(stu),字段如下:

-- 学号(stuID)

-- 姓名(stuName)

-- 性别(stuSex)

-- 入学时间(stuDate)

create table stu (

stuID number(12),

stuName varchar(5),

stuSex varchar(2) check(stuSex in(‘男‘,‘女‘)),

stuDate date

);

--二、请创建一个表,表名为phone,表结构如下

电话号码(PHONENUM VARCHAR2(8))

电话费 (PAY number(8,2))

号码等级(NUMLEVEL VARCHAR2(4))

费用日期(PAYDATE varchar2(12)

create table phone(

phonenum varchar2(8),

pay number(8,2),

numlevel varchar2(4),

paydate varchar2(12)

);

1、插入以下两条数据.

123456, 600, pt04, 20051220

888888, 900, pt05, 20051019

insert into phone (phonenum,pay,numlevel,paydate) values(123456,600,‘pt04‘,20051220);

insert into phone (phonenum,pay,numlevel,paydate) values(888888,900,‘pt05‘,20051019);

2、创建一个备份表结构名为phone_bak, 将phone中的数据插入phone_bak中.

create table phone_bak as select * from phone;

3、将电话号码为123456的电话费改为1000,执行回滚操作

update phone set paydate=1000 where phonenum=123456;

rollback;

4、在phone表中,如果PHONENUM字段中的数据为‘123456’,则返回‘Y’否则返回‘N’

select decode(phonenum,‘123456‘,‘y‘,‘n‘) from phone;

5、将phone表中的电话号码为123456的记录的费用日期(PAYDATE)字段的日期转变成YYYY/MM/DD格式

select to_date(paydate,‘yyyy/mm/dd‘) from phone where phonenum=123456;

update phone set paydate=to_date(paydate,‘yyyy/mm/dd‘) where phonenum=123456;

6、将电话号码为888888的号码等级改为pt04并提交

update phone set numlevel=‘pt04‘ where phonenum=888888;

commit;

--三、创建满足下面条件的三个数据库表

--表名:Card

作用:存储上网卡信息

-------------------------------------------------------------------

列名 数据类型 长度 是否为空 字段说明

ID varchar 10 否 主键,不允许有相同值

PassWord varchar 50 否 密码

Balance int 4 是 卡上余额

UserName varchar 50 是 持卡人姓名

-------------------------------------------------------------------

create table Card (

ID varchar(10) primary key,

PassWord varchar(50) not null,

Balance number(4),

UserName varchar(50)

);

--添加注释内容

comment on column Card.password is ‘密码‘;

comment on column Card.Balance is ‘卡上余额‘;

--表名:Computer

作用:存储计算机及状态信息

-------------------------------------------------------------------

列名 数据类型 长度 是否为空 字段说明

ID varchar 10 否 主键,不允许有相同值

CardID varchar 1 否 是否正在使用

ComputerID varchar 100 是 备注和说明信息

-------------------------------------------------------------------

create table Computer (

ID varchar(10) primary key,

CardID varchar(1) not null,

ComputerID varchar(100) not null

);

表名:Record

作用:存储每次上机的信息

-------------------------------------------------------------------

列名 数据类型 长度 是否为空 字段说明

ID numeric 8 否 主键,不允许有相同值

CardID varchar 10 否 外键,引用Card表的ID字段

ComputerID varchar 10 否 外键,引用Computer表的ID字段

BeginTime data 4 是 开始上机时间

EndTime data 4 是 下机时间

Fee data 9 是 本次上机费用

-------------------------------------------------------------------

create table Record (

ID number(8) not null,

CardID varchar(10) not null,

ComputerID varchar(10) not null,

BeginTime date,

EndTime date,

Fee date

);

foreign KEY(CardID) REFERences Card(ID)

--查看约束

select constraint_name,table_name from user_constraints where table_name =upper(‘computer‘);

--注释内容

comment on column Record.Fee is ‘本次上机费用‘;

comment on column Record.BeginTime is ‘开始上机时间‘;

comment on column Record.Fee is ‘本次上机费用‘;

comment on column Record.EndTime is ‘下机时间‘;

创建表之后,编写和实施约束,要求如下:

1、针对Record表的CardID、ComputerID字段分别与Card表、Computer表建立主外键关系(引用完整性约束)

alter table Record add constraint fk_CardID foreign key(CardID) references Computer(ID);

2、Card表中,卡上的余额不能超过1000

alter table Card add constraint ck_Balance check(balance<=1000);

3、Computer表中,OnUse只能是0或者1

alter table computer add (onUse number(2));

alter table computer add constraint ck_onUse check (onUse in(0,1));

4、Record表中,EndTime不能早于BeginTime (错的)

alter table computer add constraint ck_Time check(to_date(EndTime,‘yyyy-mm-dd hh24:mi:ss‘)>to_date(BeginTime,‘yyyy-mm-dd hh24:mi:ss‘));

--作业02修改字段

1、为stu表添加年龄字段(stuAge),类型为(number(8)),并且统一赋值为 30.

alter table stu add (stuAge number(8) default(30));

2、修改stuName字段为(varchar2(20))

alter table stu modify(stuName varchar(20));

3、删除studate字段

alter table stu drop studate;

--作业04-1 添加约束

创建班级表(clsses),字段包含:

【班级编号(cid)、班级名称(cname)】

create table classes(

cid number(8),

cname varchar2(20)

);

需求如下:

1、为“班级编号添”加主键约束

alter table classes add constraint pk_cid primary key(cid);

2、为“班级名称”添加非空约束

alter table classes modify (cname not null);

3、插入3条测试数据

insert into classes values(1001,‘蓝桥1班‘);

insert into classes values(1002,‘蓝桥2班‘);

insert into classes values(1003,‘蓝桥3班‘);

创建学生表(stus),字段包含:

【学号(stuID)、姓名(stuName)、性别(stuSex)、年龄(stuAge)、入学时间(stuDate),班级编号(cid)】

create table stus(

stuID number(8), --学号

stuName varchar2(20), --姓名

stuSex char(2), --性别

stuAge number(8), --年龄

stuDate Date, --入学时间

cid number(8) --班级编号

);

需求如下:

1、为stuID添加主键约束

alter table stus add constraint py_stuID primary key(stuID);

2、为stuName添加非空约束

alter table stus modify (stuName not null);

3、为stuSex添加默认值

alter table stus modify (stuSex char(4));--修改长度

alter table stus modify (stuSex default ‘男‘);

4、为stuAge添加检查约束 0-100

alter table stus add constraints ck_stuAge check(stuAge>=0 and stuAge<=100);

5、为stuDate添加唯一性约束

alter table stus add constraint un_stuDate unique(stuDate);

6、为cid添加外键约束

alter table stus add constraint fk_cid foreign key (cid) references classes (cid) on delete cascade; --添加外键

select constraint_name,table_name from user_constraints where table_name =upper(‘stus‘);--查看约束

alter table stus drop CONSTRAINT fk_cid; --删除外键约束

7、添加5条测试数据

insert into stus values(1801,‘Ben‘,‘男‘,21,to_date(‘2015/5/19‘,‘yyyy-mm-dd‘),1001);

insert into stus values(1800,‘BenQ‘,‘女‘,19,to_date(‘2017/2/29‘,‘yyyy-mm-dd‘),1003);

insert into stus values(1802,‘Chen‘,‘男‘,22,to_date(‘2018/6/22‘,‘yyyy-mm-dd‘),1002);

insert into stus values(1803,‘Legend‘,‘男‘,25,to_date(‘2014/8/19‘,‘yyyy-mm-dd‘),1003);

insert into stus values(1804,‘Wave‘,‘女‘,21,to_date(‘2016/5/19‘,‘yyyy-mm-dd‘),1002);

insert into stus values(1805,‘QQ‘,‘女‘,20,to_date(‘2016/7/21‘,‘yyyy-mm-dd‘),1003);

--04-2约束练习

1、创建表 emp1,字段如下

eno char(3),

ename char(6),

sex char(2),

age number(2),

dno char(3)

create table emp1(

eno char(3),

ename char(6),

sex char(2),

age number,

dno char(3)

);

2、并插入如下数据

1 ,TOM , 男 , 21 ,’001’

2 ,JERRY ,男 ,21 ,’002’

3 ,KATE , 女 ,21 ,’003’

4 ,MARY ,女 ,21 ,’004’

5 ,JACK , 男 ,21 ,’005’

insert into emp1 values(1,‘TOM‘,‘男‘,21,001);

insert into emp1 values(2,‘JERRY‘,‘男‘ ,21,‘002‘);

insert into emp1 values(3,‘KATE‘,‘女‘,21,003);

insert into emp1 values(4,‘MARY‘,‘女‘ ,21,‘004‘);

insert into emp1 values(5,‘JACK‘,‘男‘,21,‘005‘);

3、 在eno字段上创建主键约束

alter table emp1 add constraint pk_ eno primary key(eno);

4、 在ename字段上创建非空约束

alter table emp1 modify (ename not null);

5、 创建检查约束判断age在18到60岁之间的男性或者age在18到55岁之间的女性

alter table emp1 add constraint ck_age check ((sex=‘男‘) and (age between 18 and 60) or (sex=‘女‘ and age between 18 and 55));

insert into emp1 values (666,‘Legend‘,‘男‘,20,100);--测试数据

insert into emp1 values (66,‘Lily‘,‘女‘,120,100);

6、 在dno字段上创建唯一性约束

alter table emp1 add constraint un_dno unique(dno);

7、创建和emp1表字段相同的emp_bak表,将emp_bak表的eno字段与emp表的eno字段创建外键约束

create table emp_bak ad select * from emp1;

alter table emp1 add constraint fk_emo foreign key emp_bak(eno) references emp1(eno);

--作业04-3 添加约束

创建clsses表:

create table classes

(

cid number(8),

cname varchar2(20)

);

为cid添加主键约束

alter table classes add constraint pk_cid primary key(cid);

创建stus表:

create table stus(

stuID number(8), --学号

stuName varchar2(20), --姓名

stuSex char(2), --性别

stuAge number(8), --年龄

stuDate Date, --入学时间

cid number(8)

);

为stuID添加主键约束

alter table stus add constraint pk_stuID primary key(stuID);

为stuName添加非空约束

alter table stus modify (stuName not null);

为stuSex添加默认值

alter table stus modify (stuSex dafault ‘女‘);

为stuAge添加检查约束 0-100

alter table stus add constraint ck_stuAge check(stuAge between 0 and 100);

为stuDate添加唯一性约束

alter table stus add constraint un_stuDate unique (stuDate);

为cid添加外键约束

alter table stus add constraint fk_cid foreign key (cid) reference classes (cid);

原文地址:https://www.cnblogs.com/qichunlin/p/9059764.html

时间: 2024-07-29 13:26:08

2018.5.19 Oracle数据操作和管理表的综合练习的相关文章

mysql第四篇:数据操作之多表查询

mysql第四篇:数据操作之多表查询 一.多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment PRIMARY KEY, dname VARCHAR(50) not null COMMENT '部门名称' )ENGINE=INNODB DEFAULT charset utf8; #添加部门数据 INSERT INTO `dept` VALUES ('1', '教学部'); INSERT INT

4.windows和Linux下创建oracle用户名表空间,表,插入数据,用户管理表等操作

进入超级管理员,执行以下命令 Window下创建数据库,表空间,用户,插入数据等操作 -- 01 创建表空间 -- 注意表空间的路径 根据实际安装环境进行调整 CREATE TABLESPACE ts_myscott LOGGING DATAFILE 'F:/app/to-to/oradata/orcl/ts_myscott.dbf' SIZE 10M EXTENT MANAGEMENT LOCAL; CREATE TABLESPACE ts_myscott2 LOGGING DATAFILE

第八章| 2. MySQL数据库|数据操作| 权限管理

1.数据操作 SQL(结构化查询语言),可以操作关系型数据库 通过sql可以创建.修改账号并控制账号权限:  通过sql可以创建.修改数据库.表:  通过sql可以增删改查数据: 可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据以及. 1.1数据的增删改查 插入数据INSERT 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3-

Oracle 数据操作提示“记录被另一个用户锁住”

oracle数据中删除数据时提示“记录被另一个用户锁住” 解决方法: 1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b.owner,b.object_name,l.session_id,l.locked_mode from v$locked_object l, dba_objects b where b.object_id=l.object_id

4.windows和Linux下创建oracleusername表空间,表,插入数据,用户管理表等操作

进入超级管理员,运行下面命令 Window下创建数据库.表空间,用户,插入数据等操作 -- 01 创建表空间 -- 注意表空间的路径 依据实际安装环境进行调整 CREATE TABLESPACE ts_myscott LOGGING DATAFILE 'F:/app/to-to/oradata/orcl/ts_myscott.dbf' SIZE 10M EXTENT MANAGEMENT LOCAL; CREATE TABLESPACE ts_myscott2 LOGGING DATAFILE

--使用oracle数据先要创建表空间

one\--创建表空间 CREATE TABLESPACE 表空间的名字DATAFILE 'E:\oracle\app\userdata\java5space.dbf' --表空间物理文件路径SIZE 5m --初始大小AUTOEXTEND ON --是否自动增长 --创建用户create user java5user --用户名identified by "java5user" --密码default tablespace "JAVA5SPACE" --默认管理的

三十一.MySQL存储引擎 、 数据导入导出 管理表记录 匹配条件

1.MySQL存储引擎的配置 查看服务支持的存储引擎 查看默认存储类型 更改表的存储引擎 设置数据库服务默认使用的存储引擎 1.1 查看存储引擎信息 mysql> SHOW ENGINES\G 1.2 查看默认存储类型 mysql> SHOW VARIABLES LIKE 'default_storage_engine'; +------------------------+--------+ | Variable_name          | Value  | +-------------

25_MySQL存储引擎 、 数据导入导出 管理表记录 匹配条件

版本:5.7.28服务器:mysql 192.168.4.20 1.MySQL存储引擎的配置查看服务支持的存储引擎查看默认存储类型更改表的存储引擎设置数据库服务默认使用的存储引擎 查看存储引擎信息mysql> SHOW ENGINES;+--------------------+---------+-------------------| Engine             | Support |+--------------------+---------+-----------------

(七)MySQL数据操作DQL:单表查询

(1)单表查询 1)环境准备 mysql> CREATE TABLE company.employee5( id int primary key AUTO_INCREMENT not null, name varchar(30) not null, sex enum('male','female') default 'male' not null, hire_date date not null, post varchar(50) not null, job_description varcha