oracle数据库基本操作

基本操作的语句:

DML:SELECT、INSERT、UPDATE、DELETE、MERGE

DDL:CREATE、ALTER、DROP、RENAME、TRUNCATE、COMMENT

DCL:GRANT、REVOKE

事务处理控制:COMMIT(释放)、ROLLBACK(回滚)、SAVEPOINT

创建表:

create table student
(
id char(2) primary key,
thename varchar(10) not null,
password varchar(10) not null,
gender char(2),

email varchar(20)
)

删除表:drop table student

约束:数据完整性的体现,代表数据的可靠性和准确性,约束的类型有:主键约束(主键列唯一,主键一般没有任何意义)、唯一约束(该列唯一)、检查约束(限制数据类型)、默认约束(默认值)、外键约束

约束推荐命名:

主键:pk_colname 唯一:uq_colname 检查 :ck_colname

create table student
(
id char(2) primary key, ----主键约束
thename varchar(10) not null, -----非空约束
password varchar(10) not null,
gender char(2),
email varchar(20)
)
alter table student add constraint ck_gender check(gender='m' or gender='w') ---检查约束
alter table student add constraint uq_email unique(email)----唯一约束
alter table student modify(gender char(2) default 'm') ----默认约束

多表的关系:多对一、一对一

一对多的时候在多的一方加外键:

create table employee(
id int primary key,
name char(5) not null,
deptid int ---deptid 的值来自部门的id
)
create table department(
id int primary key,
name char(5) not null
)
alter table employee
add constraint fk_deptid
foreign key(deptid) references department(id);----外键deptid来自于部门表的主键

删除数据库的约束:alter table employee drop constraint fk_deptid

总结:数据肯定要有约束,数据才有准确性和可靠性,但是数据库开发时使用的约束过多又会影响其性能,所以一般都是在前端的开发进行对数据的约束,而数据库中的开发较少进行数据的约束

添加数据:

select * from employee
insert into employee(id,name) values (1,'linxi')
commit;----------------提交到数据库

原文地址:https://www.cnblogs.com/LinxiHuang/p/9245893.html