sql语言类别

SQL语言分类:

数据定义语言(DDL Data Definition):create创建、alter更改、truncate截断、drop删除

数据操纵语言(DML Data Manipulation Language):insert插入、select选择、delete删除、update更新

事务控制语言(TCL Transaction Control Language):commit提交、savepoint保存点、rollback回滚

数据控制语言(DCL Data Control Language):grant授予、revoke回收

数据定义语言DDL:

1.create table命令:

create table [schema.]table_name

(

column_name datatype,

column_name datetype,

....

)

schema表示对象的所有者,即模式的名称。如果用户在自己的模式中创建表,则可以不指定所有者名称。

table表示表的名称。

column表示列的名称。

datatype表示该列的数据类型及其宽度。

表名命名规则:

1)表名首字符应该为字母。

2)不能使用Oracle保留字来为表命名。

3)表各的最大长度为30个字符。

4)同一用户模式下的不同表不能具有相同的名称。

5)可以使用下划线、数字、字母,但不能使用空格和单引号。

Oracel和SQL Server数据库对象表之间的差异:

列数:Oracle,254; SQL Server,1024

行大小:Oracle没有限制;SQL Server,8060字节,加16字节指向每个text或image列。

最大行数:Oracle没有限制;SQL Server没有限制

表命名规则:Oracle,[schema.]table_name; SQL Server,[[[server.]database.]owner.]table_name

2.truncate table命令:

可以只删除表中的记录而不删除表结构,删除表中的所有行而不记录日志。

truncate table <table_name>;

数据操纵语言DML:(insert,select,update,delete)

1.选择无重复的行,在select命令中包含distinct子句。

select distinct stuName,stuAge from stuInfo;

2.使用列别名,列别名不会影响列的实际名称。列别名位于列表达式后面。

select stuName as “姓 名”,stuAge as 年龄,stuNo 编号 from stuInfo;

如果列别名中指定有特殊字符(如空格)的列标题使用双引号括起来。

3.利用现有表创建新表。

语法: create table <newTable_name>

as

select {* | column(s)} from <oldTable_name> [where <condition>];

复制表结构和记录:

create table newStuInfo

as

select * from stuInfo;

复制表指定列和记录:

create table newStuInfo

as

select stuName,stuAge,stuNo from stuInfo;

只复制表结构:

create table newStuInfo

as

select * from stuInfo where 1=2;

4.查看表中行数:

select count(1) from stuInfo; --1比*号效率较高

5.取出stuName,stuAge列不存在重复的数据记录:

select stuName,stuAge from stuInfo

group by stuName,stuAge

having(count(stuName||stuAge));//“||”在这里是连接操作符,类似于“+”,意思为将两部分内容连接在

一起,因为count()里面只能有一个列,所以连接起来。

6.删除stuName、stuAge列重复的行(相同数据只保留一行)

delete from stuInfo where rowid not in(

select max(rowid) from stuInfo group by stuName,stuAge

having (count(stuName||stuAGe)>1)

union

select max(rowid) from stuInfo group by stuName,stuAge

having (count(stuName||stuAge)=1)

);

7.根据当前用户所有数据量>100万的表的信息

select tabke_name from user_all_tables a where a.num_rows>1000000;

--user_all_tables为系统提供的数据视图,使用者可以通过查询该视图获得当前用户表中描述。

事务控制语言TCL:

1)commit:提交事务,提事务中对数据库的修改进行永久保存。

2)rollback:回滚事务,即取消对数据库所做的作何修改。

3)savepoint <savepoint_name>:在事务中创建存储点。

4)rollback to savepoint <savepoint_name>:将事务回滚到存储点。即savepoint 创建的存储点

开启事务:在Oracle中,上一次事务结束以后,数据第一次被修改时自动开启。

结束事务: ①数据被提交:发出commit命令;执行DDL或DCL语句后,当前事务自动commit;与Oracle分离。

②数据被撤销:发出rollback命令;服务器进程异常结束;DBA停止会话。

例: insert into dept values (10,’ACCOUNTING’,’NEW YORK’);

commit;

savepoint a;//a只是一个名称

insert into dept values (20,’SALES’,’NEW YORK’);

rollback to savepoint a;

rollback;--结果只有第一条记录插入成功

数据控制语言DCL:

数据控制语言为用户提供权限控制命令。数据库对象(如表)的所有者对这些对象拥有控制权限。所有者可以根据自己

的意愿决定其他用户如何访问对象,授予其他用户权限(insert,select,update,delete,....)

猜你喜欢

转载自blog.51cto.com/13553337/2140512