1、删除约束
alter table 表名 drop constraint 约束名;
查看约束:select * from user_constraints;(全部)
select * from user_constraints where table_name='表名(大写)';(查某一个表)
查看表结构:desc 表名
2、修改表结构
增加删除列
alter table 表名 add 列名 数据类型;
alter table 表名 drop column 列名;
修改数据类型
alter table 表名 modify 列名 新类型;
重命名
alter table 表名 rename column 列名 to 新列名 ;
alter table 表名 rename to 新表名;
3、数据的增删改查
增加数据
insert into 表名(列名,列名) values (值,值)
非number类型的数据使用单引号
值得数量和列的数量一致
非空且没有默认值大的列必须赋值,否则报错
简写语法:insert into 表名 values (值,值)
按照表的顺序给每一列赋值
insert into 表名 select…
将查询到的其他表中数据插入进去
注意:1.不能违反约束 2.不能超过数据的长度 3.先加主表,再加子表
序列
主要适合于没有实际意义,保证数据不重复,作为主键的列
先添加一个列 alter table exam add examid number(4);
自动编号 create sequence sql_exam(表名)_examid(列名)
increment by 1(序号一次增加1)
start with 10(序号从10开始)
cache 10(一次生成10个序号,用完再生成)
insert into exam values(‘10001’,‘java’,‘99’,sysdate,seq_exam_examid.nextval);
查看当前序号 select seq_exam_examid.currval from dual;
数据修改
update 表名 set 列名=值,列名=值 where 条件;
update students set sex=‘男’ where stuname=‘张三’;
update exam set score=score+5 where stuno=’’;
删除数据
delete from 表名 where 条件;(没有条件操作的是整个表)
先删除子表,在删除主表
truncate table 表名(清空表,不写日志,数据多)
delete from 表名(执行时写日志(有备份,可回复)数据少)
查询数据
查询所有数据
select * from 表;
查询空值
select * from 表名 where 列名 is null
查询非空
select * from 表名 where 列名 is not null
查询一个范围
and
select * from 表名 where 列名>10 and 列名<200;
between…and…
select * from 表名 where 列名 between 10(小值) and 200(大值)
select * from 表名 where 列名 not between 10(小值) and 200(大值)
in
select * from 表名 where 列名 in (10,20);
等于10的或者等于20的
union 合并查询结果集
两个查询列数一样
也可以用于合并两个查询结果集
查询结果智能化合并重复的行,如果不想合并,在union后加all
select * from 表名 where 列名=10
union
select * from 表名 where 列名=20;
like 模糊匹配
% 任意字符 不限长度
select * from 表名 where 列名 like ‘%S’;
_ 一个任意字符 只限一个字符
select * from 表名 where 列名 like ‘_S%’;
第二位是S
筛选
筛选行
select * from 表 where 条件;
筛选列
select 列名,列名 from 表名 where 条件;
列别名
select eeee as 姓名, 列名 as 薪水,‘中国’ as 国籍 from 表名 where 条件; 常量列
select * from 表名 where 条件;
列名 as 别名
方便查看结果,区别同名列
rowid、rownum列
隐藏的列,建表时候就有
rowid 行地址 值不变 select 表名.*,rowid from 表名
rownum 行号 值会变 select 表名.*,rownum from 表名
where条件
算术运算符:+ - * / %
关系运算符:> < >= <= != <>
逻辑运算符: and or not