</pre><p><pre name="code" class="sql">先在正在学习Oracle,整理了一下sql基本语句用于sqlplus的操作:
--给用户加锁
alter user scott account lock;
--解锁用户
alter user scott account unlock;
--创建表空间
create tablespace ts_my datafile 'D:\Eclipse_11g\oradata\yc\ts_my.dbf' size 10m;
create temporary tablespace tts_my tempfile 'D:\Eclipse_11g\oradata\yc\tts_my.dbf' size 10m;
--创建用户并给与表空间
create user xh identified by a default tablespace ts_my temporary tablespace tts_my;
--给用户附权限(登录,资源)
grant connect,resource to xh;
--创建表
create table person(
id Integer constraint pk_person_id primary key,
name varchar2(10),
tall number(3,2),
birthday date,
phone varchar2(11),
computerID Integer
);
--查询拥有的表
select table_name from user_tables;
--查看表结构
desc table_name;
--添加字段
alter table person add phone varchar2(11);
--添加一条记录
insert into person (id, name, tall, birthday, phone,computerID) values (1001, '小名', 1.92, '12-3月-92', 122344566,null);
--添加约束
--第一种:直接在字段后加
create table person(
id Integer constraint pk_person_id primary key, --添加主键约束
name varchar2(10) not null,--添加非空约束
tall number(3,2) constraint ck_person_tall check(0 < tall and tall < 3), --添加检查约束
birthday date,
phone varchar2(11) unique, --添加唯一约束
sex char(2) default '男', --添加默认值约束
computerID Integer constraint fk_person_computer_computerID foreign key(computer) references computer(id) --添加外键约束
);
--第二种:在确定字段后加
create table person(
id Integer,
name varchar2(10),
tall number(3,2),
birthday date,
phone varchar2(11),
sex char(2),
computerID Integer ,
constraint pk_person_id primary key(id), --添加主键约束
constraint pk_person_id primary key(id,name), --添加联合主键约束
constraint ck_person_tall check(0 < tall and tall < 3), --添加检查约束
constraint modify name not null, --添加非空约束
constraint uk_person_phone unique(phone), --添加唯一约束
constraint fk_person_computer_computerID foreign key(computer) references computer(id) --添加外键约束
);
--第三种:创建完表后加
create table person(
id Integer
name varchar2(10),
tall number(3,2),
birthday date,
phone varchar2(11),
sex char(2),
computerID Integer
);
alter table person add constraint pk_person_id primary key(id);--添加主键约束
alter table person add constraint pk_person_id_name primary key(id,name), --添加联合主键约束
alter table person add constraint ck_person_tall check(0 < tall and tall < 3), --添加检查约束
alter table person add constraint uk_person_phone unique(phone), --添加唯一约束
alter table person add constraint fk_person_computer_computerID foreign key(computerID) references computer(id);-- 添加外键约束
alter table person modify sex default '男';--添加默认值约束
--添加默认值约束
alter table person add sex char(2);
alter table person modify sex default '男';
--使用default调用默认值
insert into person (id, name, tall, birthday, phone, sex) values (1006, '小名', 1.92, '12-3月-92', 1223423566,default);
--不插入此字段,默认就调用默认值
insert into person (id, name, tall, birthday, phone) values (1007, '李名', 1.92, '12-3月-92', 1223423536);
--修改表数据
update person set phone = '1234566789'; --修改Person表中所有数据
update person set phone = '1234566789' where id = 1001; --修改Person表中指定数据
--查询用户的约束
select owner,constraint_name, constraint_type,table_name from user_constraints;
--外键约束
--添加新表
SQL> create table computer (id Integer primary key, brand varchar2(10), price float);
--给person添加computerID字段
alter table person add computerID Integer;
--要建立主外键关系,要分清主表和子表
--判断条件:关联字段,如果是主键就是主表,如果外键就是子表
alter table person add constraint fk_person_computer_computerID foreign key(computerID) references computer(id);
insert into computer (id, brand) values (10001, 'lenovo');
insert into person (id, name, tall, birthday, phone, sex,computerID) values (1008, '小白', 1.92, '12-3月-92', 1223423161,default,10001);
--删除约束
alter table person drop constraint fk_person_computer_computerID ;
--删除表数据 先删子表,再删主表
delete from person where computerID = 10001;
delete from computer where id = 10001;
--外键约束的特征:
--插入数据时:关键字段值必须先在主表中有,然后才能插入子表
--删除数据时:必须先删除子表中与关联字段有关的数据,然后才能删除主表中的数据
--修改字段所占空间大小
col column_name format a长度; --只针对字符串
col name format a10;
col column_name format 9999; --只针对数字,几位数字就有几个9
col id format 9999;
--清除修改字段所占空间大小
col column_name clear;
--对约束的操作
--禁用约束
alter table person disable constraint fk_person_computer_computerID
--激活约束
alter table person enable constraint fk_person_computer_computerID --不允许有违反约束的数据
alter table person enable nova constraint fk_person_computer_computerID--不允许有违反约束的数据