文法
1、建表
create table 表名(
列名 資料類型,
……
);
2、删除表:drop table 表名;
3、添加列:alter table 表名 add(列名 資料類型);
4、修改列:alter table 表名 rename column 原列名 to 列名;
5、修改資料類型:alter table 表名 modify 列名 資料類型;
6、删除列:alter table 表名 drop column 列名;
7、添加注釋
添加表注釋:comment on table 表名 is '表注釋;
添加字段注釋:comment on column 表名.列名 is '列注釋';
8、添加限制
添加主鍵限制:alter table 表名 primary key(列名);
添加唯一限制:alter table 表名 constraint 限制名 unique(列名);
(主鍵限制和唯一限制的差別:主鍵限制:唯一辨別,不能為空。唯一限制:唯一辨別,隻能有一個值為空)
非空限制:alter table 表名 modify(列名 constraints);
9、插入資料:insert into(列名,……)values(資料,……);
注意,oracle中不能直接寫入日期函數
插入時間:to_date('2018-1-4 15:53:34','YYYY-MM-DD HH24:MI:SS')
插入目前時間:sysdate
下面是我做的一個例子,應用到了上面的文法:
1 --student表
2 create table student(
3 stu_id varchar2(10) primary key,
4 stu_name varchar2(10) not null,
5 stu_sex varchar2(2) not null,
6 stu_birthday date,
7 class_id number
8 );
9 --添加表注釋
10 comment on table student is '學生資訊表';
11 --字段添加注釋
12 comment on column student.stu_id is '學号(主鍵)';
13 comment on column student.stu_name is '學生姓名';
14 comment on column student.stu_sex is '學生性别';
15 comment on column student.stu_birthday is '學生出生年月';
16 comment on column student.class_id is '學生所在班級';
17
18 --sclass表
19 create table sclass(
20 class_id number primary key,
21 class_name varchar2(10) not null
22 );
23 comment on table sclass is '班級資訊表';
24 comment on column sclass.class_id is '班級編号';
25 comment on column sclass.class_name is '班級名稱';
26
27 --添加外鍵
28 alter table student add constraint fk_class_id foreign key(class_id) references sclass(class_id);
29
30 --添加資料
31 insert into sclass(class_id, class_name)values(1,'計應1401');
32 insert into sclass(class_id, class_name)values(2,'計網1401');
33 insert into sclass(class_id, class_name)values(3,'軟體1401');
34 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A001','張珊','女',to_date('1995-10-02','yyyy-mm-dd'),1) ;
35 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A002','李思','女',to_date('1995-10-02','yyyy-mm-dd'),1) ;
36 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A003','王武','女',to_date('1996-10-02','yyyy-mm-dd'),2) ;
37 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A004','趙柳','女',to_date('1996-12-02','yyyy-mm-dd'),3) ;
38 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A005','趙柳','女',sysdate,3) ;