oracle--创建数据库

创建表:

create table 表名(

字段名1 数据类型 约束条件,

字段名2 数据类型 约束条件,
.。。。
字段名n 数据类型 约束条件

);

表名: 开头必是字母,1--30字符, 字母,数字,下划线,$ ,#
字段名1 表名唯一, 关键字不能为表名


插入表记录
1. insert into 表名 values(值1, 值2, 值3,值4,...)

insert into myTA values(1000, '李四张山', '男');

2.insert into 表名(字段名1, 。。。) values(值1,。。。)
insert into myTA(name) values('赵武');

修改表数据
update 表名 set 字段名1=值(表达式), 字段名2=值(表达式),。。。 where 修改条件
update myTA set sex='女';//
update myTA set sex='F' where name='abcdefse' ;
update myTA set id = id+11, sex='MF' where name='李四张山';

删除
1.delete from 表名 where 删除条件 ;//删除表记录

truncate table 表名; //重新建立表结构, 
delete from 表名;//没有修改表结构

2.drop table 表名; //删除整个表的


约束
1、主键 primary key 唯一值,并不能为空,一个表只能有一个主键

create table myB(
id number(4) primary key,
name varchar2(8),
sex char(2)
);


2、非空约束 not null , 不能为空


create table myC(
id int primary key,
name varchar2(8) not null,
sex char(2) null
);


3、唯一约束 unique,赋值时值是唯一的,但空值可以出现多次

create table myD(
id int primary key,
name varchar2(8) unique ,
sex char(2) null
);

primary key == unique not null
create table myD(
id int primary key,
name varchar2(8) unique not null,
sex char(2) null
);

4、检查约束:check(),满足条件的才可以赋值成功

create table myE(
id int primary key,
name varchar2(8) unique ,
age int check(age>20 and age<60),
age int check(age beween 20 and 60),
sex char(2) check( sex in ('M','F') )

);

5、外键, 外键的值必需是主键中以存在的值


主表的字段名是唯一或主键
向子表中插入数据时,值必须是主表存的值, 但可以是空值(多个)


create table mySoct(
sid int,
mathc number(4),
chinec number(4),
constraint 外键名 foreign key(从表字段名) references 主表名(主表的字段名)
);

先删除从表,再删除主表

create table status(
status_id number(3) primary key,
status_name varchar2(8)
);

insert into status values(101, '创建');
insert into status values(102, '接受');
insert into status values(103, '单派');
insert into status values(104, '备餐');
insert into status values(105, '送出');
insert into status values(106, '结账');
insert into status values(107, '无人接单');

select * from status; 


create table order_header(
order_id number(1),
order_code number(15),
status_id number(3),
order_date varchar2(8),
product_id varchar2(4),
qty number(3),
amount number(5),

foreign key(status_id) references status(status_id)
); 
insert into order_header values(1, 201107010001, 101, '20110701' ,'D101',1, 80); 
insert into order_header values(2, 201107010002, 106, '20110701' ,'D118',5, 125); 
select * from order_header;

每个业务日期的“状态为'单派','送出','结账”的订单金额合计值。
select order_header.order_code, order_header.status_id, order_header.qty*order_header.amount from status inner join order_header on status.status_id=order_header.status_id 
where status_name in ('单派','送出','结账')

表结构
1.加添表结构
alter table 表名 add(新字段名 数据类型 约束, sex char(2) 。。。);


2.修改表结构:

alter table 表名 modify (字段名 数据类型 约束,。。。);
数据类型:类型, 类型的取值范围大


3.删除表结构

alter table 表名 drop column 字段名;//删除一列


alter table 表名 drop ( 字段名, 字段名,。。。);//删除多列

4.修字段名
alter table 表名 rename column 旧字段名 to 新字段名;

5.修改表名
rename 旧表名 to 新表名;

6.显示表结构:
desc 表名

日期类型:date

1.to_date('日期字符串','格式字符串')

to_date('2000-02-5 12:30:05','yy-mm-dd hh24:mi:ss')

sysdate当前系统时间
insert into w1 values(1005, 'www', sysdate);

3.to_char( 日期字段名, '字符串的格式')

select * from myGg where to_char(eb,'dd-mm-yyyy' )='03-10-2018';

2.months_between(日期数据, 日期数据),两个日期之间的月份
select months_between(日期数据, 日期数据) from myGg;

4.to_char( 数字段名, '字符串的格式')

select to_char(pr, '$999,999,999.99') from mygf;

L:当地货币符号

$:美元符号
9:一个数字
0:强制0显示

猜你喜欢

转载自www.cnblogs.com/fqqwz/p/11636756.html