1.添加新的字段
语法:alter table 表名 add (新的字段 字段类型, 是否为空);
eg: stu表中添加deptno的字段
alter table stu add(deptno number not null);
2.修改字段
语法:alter table 表名 rename column 旧列名 to 新列名;
eg: stu表中的CHASSID修改为CLASSID
alter table stu rename column CHASSID to CLASSID;
3.修改字段类型
语法:alter table 表名 modify (列名 新类型,...);
eg: stu表的salary的类型修改为number(5,2)
alter table stu modify (salary number(5,2));
4.删除字段
语法:alter table 表名 drop column 字段名;
eg: 删除stu表中的gender字段
alter table stu drop column gender;
5.truncate:删除表中的数据 (不会记录日志,删除后不能恢复)
语法: truncate table 表名;
注意: truncate只是清空表中的数据,但是保留表的结构,删除表用drop
6. drop:将表结构和数据全部删除 (不会记录日志,删除后数据不能恢复)
语法:drop table 表名
7. 数据查询
select * from stu --查询stu表中所有的数据
select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]
查询stu表中一班的学号,按照sage升序排列
eg:select sno from stu where class='一班' order by sage asc;
8. 新增数据 insert
语法:insert into 表名 values()
eg: 往stu表中添加sno,sname,sage数据
insert into stu values(1,'张三',18);
9. 修改记录 update
|-语法:update 表名 set 字段名=值[,....] [where 过滤条件];
set后面的字段都是要修改的值
eg:update stu set stuname='张珠',gender='女',age=17 where stuid=10
update t_emp set name='jerry';
//将id=1001的用户名改为 jerry,工资改为 888
update stu set name='jerry',salary=888 where id=1001;
10. 删除数据delete(会记录日志,删除的很慢)
delete from 表名 where 条件 ----删除满足条件的记录
delete from stu where sno = 1; ---删除stu表中sno=1的学生
delete from stu----删除表中所有数据
commit; ----提交数据
rollback; ----回滚数据
delete可以恢复删除的数据,如果提交了,就没办法了, delete在删除的时候,会记录日志 删除的很慢
11.重命名表
语法: alter table 表名 rename to 新表名
eg:alter table stu rename to student;
12.添加主键
要求: 往stu表中添加sno为主键
alter table stu add primary key(sno);
13.删除主键
要求:把stu表中sno主键删除
alter table stu drop primary key(sno);
14.创建索引
(1.)唯一索引(unique)
create unique index 索引名 on 表名 (字段名)
(2.)创建单列索引:
单列索引触发规则:条件必须是索引列中的原始值
语法:create index 索引名 on 表名(字段);
要求:在emp表中创建一个id_ppq索引
create index id_ppq on emp(ename);
(3.)复合索引:
语法:create index 索引名 on emp(字段1,字段2);
eg:create index id_ppq on emp(ename,job);
复合索引第一列为优先检索列 即ename优先
如果要触发复合索引必须包含有优先检索列中的原始值
(4.)修改索引名
alter index 旧索引名 rename to 新索引名;
(5.)删除索引
语法:drop index 索引名
(6.)删除多个索引
drop index 索引名1,索引名2,索引名3;
15.创建视图
语法: create view +另起表名 as select 字段1,字段2 ,字段3 from 表名;
(1.)要求在stu表中创建一个视图 t_stu,去查询name和sno两个数据。
eg: create view t_stu as select name,sno from stu
(2.)简单视图可以修改
eg:update 表 set name='张三' where sno=3;
(3.)要想视图只读不修改在语句后面加上with read only;
eg:create view +另起表名 as select 字段1,字段2 from 表名 with read only;
16.删除视图
语法:drop view 视图名
要求:删除stu表中的视图t_stu
drop view t_stu;
17.创建用户
要求:创建一个tom用户密码是123456
create user tom identified by 123456;
18.数据复制
(1.)当已经有一个stu1表了,在创建一个相同数据和结构的表stu2
create table stu2 as select *from stu1;
(2.)创建一个与t1相同但是没有数据的t3表 自己在重新插入数据insert into .......
create table t3 as select *from t1 where1>2;
where后面的条件只要不满足都行 如:3>4, 5>6 ,1>1等等等...
(3.)原本t2中没数据,通过t1把数据直接放到t2中 ,t1 和 t2表字段是一样的
insert into t2 select *from t1;
(4.)复制指定字段
要求:只复制teacher2中的id和 name到teacher1中
create table teacher1 as select id, name from teacher2 where 1>1;