1.创建表
create table <表名>( <列名> <数据类型> [列级完整性约束条件],<列名> <数据类型> [列级完整性约束条件],…[<表级完整性约束条件>]);
例:创建学生表
create table student (
stuid number (10),
stuname varchar2(10),
sex char(4)
);
2.删除表 语法格式: drop table <表名>
注意:在删除表时,如果该表中的数据被另外表的外键引用,那么必须要先删除外键表,才能删除该表。
例:drop table student;
3.插入信息 语法格式:insert into <table>[(column1[,column2,…])] values(value1[,value2,…]);
insert into student(stuid,stuname ) values(1,'李大');
insert into student values(2,'李小','男');
commit;
4.查询表 select * from <表名>;
例: select * from student
表的约束
1.非空约束(not null):该字段的值不能为空。
2.唯一约束(unique):该字段的值不能重复。
3.主键约束(primary key):唯一的确定表中的每一行数据
4.检查约束(check):检查约束指表中的某一个列或一组列取值应该满足检查约束指定的条件。
5.缺省约束(default):为字段提供默认值
6.外键约束(foreign key):外键是指引用另一个表中的某个列或某几个列、或本表中另一个列或几个列。
修改表(修改表 关键字:alter — modify(修改) 非空、缺省 drop(删除) constraint(加约束))
1.增加列 语法格式:alter table <表名> add <新列名> <数据类型>;
alter table student add address varchar2(30);
alter table student add score number (3,1); --逗号前 3个数字,逗号后1位小数
2.修改数据类型 alter table <表名> modify 字段名 字段类型;
alter table student modify address char(30);
3.删除列 alter table <表名> drop column 字段名;
alter table student drop column address;
4.非空约束 alter table <表名> modify 字段名 not null;
alter table student modify stuname not null;
5.唯一约束 alter table <表名> add constraint 建名 unique(字段名);
alter table student add constraint unique_id unique (stuname);
6.主键约束 alter table <表名> contraint 建名 primary key (字段名);
alter table student add constraint pk_id primary key (stuid);
7.检查约束 alter table <表名> add contraint 建名 check(1 or 2);
alter table student add constraint check_id check(sex = '男' or sex = '女');
8.缺省约束 alter table <表名> modify 字段名 default '济南';
alter table student modify address default '济南';
9.外键约束 alter table <表名> contraint 建名 references <表名>(claid);
alter table student add constraint fk_id claid references clazz(claid);
10.修改表名
方式1: alter table <表名> rename to <新表名> ;
alter table student rename to t_student;
方式2: rename <表名> to <新表名>;
rename t_student to student;
11.修改列名
alter table <表名> rename column <新表名> to 新字段名;
alter table student rename column stuname to stunam11;