SQL语言(结构化查询语言)

xiaoxiao2021-02-28  44

 用途:通过DBMS来操作DB

1.DDL语言(Data Definition Language):数据定义语言

        create:用来创建表和对象

--创建student表, -- 字段 stu_id number(4), -- stu_name varchar2(20), -- stu_gender varchar2(2). create table student( stu_id number(4), stu_name varchar2(20), stu_gender varchar2(2) );

        alter:用来修改表结构

--在student表中增加一个字段 stu_score number(4,1) alter table student add (stu_score number(4,1)); --将字段stu_gender改为stu_sex; alter table student rename column stu_gender to stu_sex; --修改stu_name的表类型为varchar2(30); alter table student modify stu_name varchar2(30);

        drop:用来删除表结构

--删除表student drop table student;

        truncate:用来清空表数据(数据不能进行还原),保留表结构

--清空student表中数据 truncate table student;

2.DML语言(Data Manipulation Language)

        用来操控数据,也就是增删改操作

        insert:用来向表中增加数据

insert into student (stu_id,stu_name,stu_sex,stu_score) values (0001,'zhangsan','m',99.9); insert into student values(0002,'lisi','m',86.4); insert into student (stu_id,stu_name,stu_sex) values(0003,'wangwu','f');

        delete:用来向表中删除数据

--删除所有数据 delete student; --删除stu_id为0003的学生数据 delete student where stu_id=0003;

        update:用来修改表中的数据

--修改所有学生的分数为60分 update student set stu_score = 60.0; --修改表中0003的分数为100分 update student set stu_score = 100.0 where stu_id=0003;

       在使用DML语言时,会触发一个事务

3.DQL语言(Data Query Language)

        用来查询数据

        select:用来查询数据

--查看所有数据 select * from student; --查看stu_name为lisi的成绩; select stu_name,stu_score from student where stu_name='lisi';

            在select语句中,可以给查询的字段起别名(使用关键字as,as可以省略),别名也可以用汉字,但是必须使用""。

--查看stu_name为lisi的成绩,并为成绩起别名score; select stu_name,stu_score as score from student where stu_name='lisi'; select stu_name,stu_score score from student where stu_name='lisi'; --查看stu_name为lisi的成绩,并为成绩起别名“分数”; select stu_name,stu_score as "分数" from student where stu_name='lisi'; select stu_name,stu_score "分数" from student where stu_name='lisi';

4.TCL语言(Tool Command Language):事务操控语言

        commit:提交数据

--提交数据 commit;

        rollback:回滚数据

        savepoint:保存点

5.DCL语言(Data Control Language):数据控制语言

        grant:授权

        revoke:撤回权限

        create user:创建用户

转载请注明原文地址: https://www.6miu.com/read-2623753.html

最新回复(0)