1.oracle之表管理sql

/*
数据类型
1. number(M,N)   整数位和小数位最多是M,其中小数位为N位
2. char(M):定长字符串,长度为M,如果插入数据时长度小于M,则在末尾补上空格
3. varchar2(M):不定长字符串,最大长度是M,如果插入数据时长度小于M,则按实际的数据插入,不作不空格的处理
4. date:日期
*/

/*
约束:约束是作用在表的字段上
1. 主键约束 primary key:唯一标识一行数据,唯一+非空
2. 非空约束not null:不能为空,不能往非空约束的列中插入null,空字符串''。如果表中已经有数据行,新增列时不能添加非空约束
3. 唯一约束unique:不允许有重复值,其中null是可以有重复
4. 默认约束default:默认值,如果插入数据时没有指定值则插入默认值,如果指定了值则取指定的值;只能在插入数据时起作用
5. 检查约束check: 字段值必须满足检查要求
6. 外键约束foreign key: 在其他表中是主键;插入数据时,外键字段值必须是主表存在的数据;主键表中数据被外键表引用,则主键表中数据不允许删除
*/


--新建表
/*
create table 表名(
列名1  数据类型  约束,
列名2 数据类型  约束,
列名3  数据类型  约束,
列名4  数据类型  约束,
列名N  数据类型  约束
);
*/
create  table  numberTest(
nn number(5,2) primary key,   -- number(5,2)最小可以存放-999.99,最大是999.99
cc varchar2(20)  not null,
ee  date  unique,
ff  varchar2(10) default 'hello',
gender  varchar2(10)  check (gender='male'  or  gender='female')
);


--查询数据teacherinfo中的所有数据
select * from teacherinfo;


--插入数据
insert into teacherinfo values(2,'Lily','dsdfsfhsdhf',12);

--删除数据
delete from teacherinfo where ff=‘hello’;

--删除表
--drop  table  表名;
drop   table subject;

--修改表结构
--alter table  表名  add/ modify / drop column   列名 数据类型(长度)  约束;

--新增一列
alter table  numberTest  add  tf varchar2(10) unique;

--修改列属性
alter table numberTest modify tf  varchar2(20); --修改列数据类型、长度时,原有的约束不会变更

--修改字段名
--alter table 表名 rename column  原字段名  to 新字段名;
alter table numberTest rename column  tf  to tff;

--删除字段
--alter table  表名 drop column 字段名;
alter table  numberTest drop column tff;
--外键的使用
create table teacherinfo(
teacherID number primary key,
teacherName  varchar2(20),
teacherZC varchar2(100),
teacherJL number
);

create table subject(
subjectId number primary key,
subjectName  varchar2(20),
subjectKS   number,
teaID  number  references  teacherinfo(teacherID)   --列名  数据类型   references 主表表名(主表主键名)
);

原文地址:https://www.cnblogs.com/cmnz/p/9210671.html