Oracle 操作表结构基本语法及示例

1.操作表

1.1创建不带主键的表

    create table student (

    studentid int,

    studentname varchar(8),

    age int);

1.2 创建表的同时创建主键约束

(1)无命名

    create table student (

    studentid int primary key not null,

    studentname varchar(8),

    age int);

(2)有命名

    create table students (

    studentid int ,

    studentname varchar(8),

    age int,

    constraint yy primary key(studentid));

1.3 删除表中已有的主键约束

(1)无命名

    可用 SELECT * from user_cons_columns;

    查找表中主键名称得student表中的主键名为SYS_C002715

    alter table student drop constraint SYS_C002715;

(2)有命名

    alter table students drop constraint yy;

1.4 向表中添加主键约束

    alter table student add constraint pk_student primary key(studentid);

1.5 重命名表

    alter table table_name rename to new_table_name;

2.操作字段

创建一个test1表:

    create table test1

    (id varchar2(20) not null);

2.1 添加字段

    语法:alter table tablename add (column datatype [default value][null/not null],….);

    示例:

        增加单个字段:

            alter table test1 add (name varchar2(30) default ‘无名氏’ not null);

        新增多个字段:

            alter table test1

            add (name varchar2(30) default ‘无名氏’ not null,

            age integer default 22 not null,

            has_money number(9,2)

            );

2.2 修改字段

    语法:alter table tablename modify (column datatype [default value][null/not null],….);

    示例:

        修改字段类型或长度:

            Alter Table 表名  modify (字段名称  (新的)字段类型);

            alter table test1 modify (name varchar2(16));

            注意:当此列有数据时,不能修改类型,不能将字段的长度减小,只能增加长度。

        修改字段名称:

            Alter Table 表名 rename  column (旧的)字段名称  to (新的)字段名称;        

            alter table test1 rename column  name to  new_name;

2.3 删除字段

    语法:alter table tablename drop (column);

    示例:
         删除字段:
        alter table test1 drop column name;

猜你喜欢

转载自blog.csdn.net/qq_34996727/article/details/81011482