Oracle数据库基础(二)

1.表名命名规则:必须以字母开头,不能超过30个字符,不要有Oracle保留字
   2.数据类型
     字符型:
        char :2000个字符   定长  效率高
        varchar2:4000个字符  变长
        Clob:字符型大对象 4G
     数字型:
        number  范围:-10^38~10^38
            sal   number(5)  -99999~99999
            sal   number(7,2) -99999.99~99999.99   7位有效数字,2位小数
     日期类型:
        date  年月日  时分秒
        timesatmp  对date类型的拓展  毫秒
     图片
        blob  二进制数据  存放图片,音频等
        一般不对图片和音频进行存储,而是存储其路径
      
   3.建表语句
    
     create table student(id number(2),name varchar2(10),sex char(4),birthday date,sal number(7,2),comm number(3),class_number number(2));
    
     添加字段:
        alter table tableName add(字段名 数据类型(长度)))
        alter table student add(score number(3));
       
     修改字段长度/数据类型/字段名(前提:保证修改的表内不能存数据)
        alter table tableName modify(字段名 数据类型(长度));
        alter table student modify(comm number(4));
        alter table student modify(class_number varchar2(4));
       
     删除字段:
        alter table tableName drop column 字段名
        
     重命名
        rename oldName to newName
        rename student to stu
        
     删除表
        drop table tableName
    
   4.对数据操作
     4.1增加数据:
   每个字段都有数据:insert into tableName (id,name,age,sex,score,sal,comm,class_number,birthday) values(2,’andy’,30,’女’,21,567,12,1,’21-2月-92′);                                                     
        
   修改时间格式:alter session set nls_date_format=’yyyy-mm-dd’;
        
 部分字段有数据:insert into student(id,name,age,sex,birthday,sal) values(7,’Bob’,30,’男’,’1983-4-24′,9483);
                     inset into student(id,name,age,sex,birthday,sal) values(8,’ku’,29,’男’,’1929-3-12′,null);   

     查找工资为空的人:select * from student where sal is null;                        
              不为         select * from student where sal is not null;
   
     4.2修改数据
        修改单个字段:update student set id=2 where name=’james’;
        修改多个字段:update student set score=80,sal=10000 where name=’james’;
              将女性工资下调一半:update student set sal=sal/2 where sex=’女’;
              
     4.3删除数据
        delete from student;   删除所有数据  可回滚
        
        savepoint  
        rollback to  设置节点,可将删除的操作回滚
        
        truncate table student   不可回滚
        
        drop table student  删除表中数据和表结构
        
        delete froom student where name=’ku’;
    
     4.4查询数据
        
        查表结构:desc table student;
        查询所含有的列:select * from student
        查询指定的列: select name from student;
        
        查询多行:select name,age from student;
        
        set timming on;
        
        
        
        查询gai的工资和年龄:
           select sal,age from student where name=’gai’;
           
        查询gai的年薪:
           select sal*12 from student where name=’gai’;
        给年薪起别名
           select sal*12 “年薪” from student where name=’gai’;   
        字段拼接:
        select name||age from student;
        select name||’的年龄是’||age from student;
        查询所有同学年终工资:
        select name||’工资’||sal*12+comm*12 from student;
        (奖金为空,年工资为空)==>
            处理null值:nvl(comm,0)*12
        select name||’工资’||sal*12+nvl(comm,0)*12 from student;
        
        where字句
        查询工资低于5000的学生:
            select name from student where sal<=5000;
            
        查询生日在1990-1-1后的学生:
            select name from student where birthday>’1990-1-1′;
            
        like字句(%可代表0-n个任意字符,_可代表任意单个字符)
            select sal,name from student where name like ‘s%’;
            
        in字句:
           查询3班所有学生姓名:
              select name from student where class_number in 3;

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐