oracle语言基础

一、语言分类

1.DML(Data Manipulation Language,数据操作语言):用于对数据的操作。

  DML包括:(1)SELECT:查询数据

select * from temp;

         (2)INSERT:增加数据到数据库

insert into temp values(4,'王五');

         (3)UPDATE:用于从数据库中修改现存的数据

update temp set name='朱亮' where name='王五';

       (4)DELETE:用于从数据库中删除数据

delete from temp where id=3;--from可以省略,不加where子句时,删除表中所有数据。

2.DDL(Data Definition Language,数据定义语言): 用于定义数据的结构,比如 创建、修改或者删除数据库对象。

  DDL包括:(1)CREATE:创建

create table temp(
id int,
name varchar2(50)
);
--从已有数据创建新表
--create table 新表名as Select 列名 from 源表名;
create table temp as select * from student;
--查看表的信息 user_tables
select * from user_table;

         (2)ALTER:修改

--修改列的属性
--alter table 表名 modify 列名 属性
alter table temp modify name varchar2(40);--更改数据类型时,要修改的列必须为空
--添加新列
--alter table 表名 add 列名 属性
alter table temp add age int;
--给列重命名
--alter table 表名 rename column 旧列名 to 新列名
alter table temp rename column name to 姓名;--name为旧列名,姓名为新列名
--删除列
--alter table 表名 drop column 列名;
alter table temp drop column age;

         (3)DROP:删除

drop table temp;

         (4)MODIFY:修改列属性

alter table temp modify name varchar2(40);--更改数据类型时,要修改的列必须为空

           (5)RENAME:重命名

--给列重命名
alter table temp rename column name to 姓名;--name为旧列名,姓名为新列名
--给表重命名
--rename 旧表名 to 新表名
rename temp to temp1;

         (6)comment:添加备注

--comment on table 表名 is '备注信息'
comment on table temp is '临时表';
----查看表的备注:user_tab_comments
select * from user_tab_comments;

3.DCL(Data Control Language,数据控制语言):用于定义数据库用户的权限。

  DCL包括:(1)GRANT

grant connect to test;

       (2)REVOKE

revoke connect from test;

二.常见问题

  1.char与varchar2区别:char为固定长度的字符串,存储的字符串长度小于规定的字符串长度时,用空格填充。varchar2为变长字符串,存储的字符串长度小于规定的字符串长度时,不用空格补充。

  2.在oracle中,使用查询时,必须使用“select ... from ...”完整语法,当查询单行数据时,from后面使用dual表,dual表在系统中只有一行一列,该表就是为了输出单行数据时语法的完整而使用的。

  3.字符类型的值区分大小写,所有的表名列名存储后为大写。

  4.所有的非数值类型的值都用单引号引起来。

原文地址:https://www.cnblogs.com/zlbx/p/4820499.html