創建表:
create table 表名 (
字段名1 字段類型 默認值 是否為空 ,
字段名2 字段類型 默認值 是否為空,
字段名3 字段類型 默認值 是否為空,
......
);
創建一個user表:
create table user (
id number(6) primary key, ---主鍵
name varchar(50) not null, ---姓名 不為null
sex varchar2(6) default '男' check ( sex in ('男','女')) ---性別 默認'男'
);
修改表名:
rename 舊表名 to 新表名;
rename user to newuser;
刪除表:
delete from 表名;
delete刪除數據是一條一條的刪除數據,后面可以添加where條件,不刪除表結構。注意:如果表中有identity產生的自增id列,delete from后仍然從上次的數開始增加。
truncate table 表名;
truncate是一次性刪掉所有數據,不刪除表結構。注意:如果表中有identity產生的自增id列,truncate后,會恢復初始值。
drop table 表名;
drop刪除所有數據,會刪除表結構。
修改表:
添加新字段:
alter table 表名 add(字段名 字段類型 默認值 是否為空);
alter table user add(age number(6));
alter table user add (course varchar2(30) default '空' not null);
修改字段:
alter table 表名 modify (字段名 字段類型 默認值 是否為空);
alter table user modify((age number(8));
修改字段名:
alter table 表名 rename column 列名 to 新列名;
alter table user rename column course to newcourse;
刪除字段:
alter table 表名 drop column 字段名;
alter table user drop column course;