SQL基础数据库语法(材料)_数据库及sql基础
SQL基础数据库语法(材料)由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“数据库及sql基础”。
一、表空间操作
1.创建表空间
Create tablespace xxx(表名)Datafile ’x:xxxxxx.dbf(文件地址)’ Size 10m(文件大小)autoexdend on;2.删除表空间
Droptablespacexxx(表空间名); 删除表空间及文件
Drop tablespace xxx including contents and datafiles;
二、用户操作
1.创建用户
Create user xxx(用户名)identified by xxx(密码)Default tablespace xxx(表名);2.删除用户
Drop user xxx(用户名)cascade;3.授予权限
Grantconnect,resource(权限)to xxx(用户名);Grant dba(最高权限)to xxx(用户名);Grant select on scott.emp to xxx(用户名);Grant update on scott.emp to xxx(用户名);撤销权限
Revoke connect,resource(权限)fromxxx(用户名);4.用户登录
Connxxx(用户名)/xxx(密码)@xxx(表空间名);
三、表数据操作
1.创建表结构语法
1)创建表结构
CREATE TABLE student(stu_no CHAR(4)PRIMARY KEY NOT NULL,--学号,主键,非空
stu_name VARCHAR2(30)NOT NULL,--姓名,非空
stu_id VARCHAR2(18),--身份证号,代表18位整数
stu_gid Number(10),--年级
stu_age NUMBER(3,0)--年龄);2)复制表结构
利用现有表创建新表,select 后边有多少字段,新表将有多少个字段,主键不会被创建
CREATE TABLE stu_info AS SELECT stu_no,stu_age FROM student;3)修改表结构
修改student表,添加stu_seat(座号)和stu_Addre(住址)两个列
ALTER TABLE student ADD(stu_seat NUMERIC(2,0),stu_addre VARCHAR2(20));修改student表的列的定义 ALTER TABLE student modify(stu_name VARCHAR2(50), stu_addre VARCHAR2(100));删除student表中stu_ addre和stu_seat列 ALTER TABLE student drop(stu_addre, stu_seat);修改student表列名
Alter table student rename column stu_no TO rempno;
4)删除表结构
删除student表结构及数据 这种删除的情况,可以恢复; Drop table student;只删除表数据,每条删除的记录不写日志,省资源,效律比delete高
但是,绝对情况下都不使用truncate删除表,因为不可以恢复。Truncate table student;5)添加注释
Comment on table student is '学生表';Comment on column student.name is '名称';2.插入表数据
往student表插入数据 Insert into
student(stu_no,stu_name,stu_id,stu_gid,stu_age)values('a001','张大','44***92111',10,19);insert into student(stu_no,stu_name,stu_id,stu_gid,stu_age)values('a002','张二','44***92112',10,20);insert into
student(stu_no,stu_name,stu_id,stu_gid,stu_age)values('a003','张三','44***92113',10,20);commit;最后的commit提交数据到数据库,否则只是在缓存插入了表数据。
3.删除表数据
根据条件删除表数据
Delete from student where stu_no='a004';请空表,不写日志,省资源,效律高,属于数据定义语言 Truncate table student;4.修改表数据
修改学号为‘a002’学员的名字为‘李二’
Update student set(列)stu_name='李二' where stu_no='a002';所有学员的年龄都加1 Update student set(列)stu_age=stu_age+1;5.查询表数据
order bygroup bywherehavinglikein 等子查询方法 1)distinct去重复查询
查询(结果)无重复数据行,distinct关键字使用:
查询结果包括学员名字和年龄,如果存在学员名字和年龄都一样的多条记录将只返回一条
-->Select distinctstu_name,stu_age from student;2)Order by结果排序
Order by 语法默认是升序,加desc语法表示降序。E:工的姓名按首字母排序,并写出姓名的长度(length)。select last_name,length(last_name)from employees order by last_name;降序:
Select last_name,length(last_name)from employees order by last_name desc;