当前位置:Gxlcms > 数据库问题 > Oracle数据库基础--建表语法+操作

Oracle数据库基础--建表语法+操作

时间:2021-07-01 10:21:17 帮助过:11人阅读

  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) ;

 



 

 

 

 

Oracle数据库基础--建表语法+操作

标签:nts   删除   arc   style   san   strong   var   column   插入数据   

人气教程排行