[教材]oracle中字段类型为char时的题目
Oracle中字段类型为char时的问题
一、首先查看一下表结构,发现字段NAME,JOB的数据类型为char。 SQL> desc employee;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
ID NUMBER(18)
NAME CHAR(20)
EMPNUM NUMBER(10)
JOB CHAR(20)
二、我们先来执行插入数据。
1、执行语句,成功插入,但发现123没有加引号也能成功插入,说明字段类型为char时,插入一串数字,可以不加引号。
SQL> insert into employee values(1004,'hihi',2002,123); 已创建 1 行。
2、发现执行下面的插入语句时,报错,这是因为NAME为char类型,而插入的数据没有
加引号,所以为char时,插入的不是纯数字串,则必须加引号。
SQL> insert into employee values(1004,hihi,2002,123);
insert into employee values(1004,hihi,2002,123)
*
第 1 行出现错误:
ORA-00984: 列在此处不允许
3,加双引号也报错,说明只能是单引号。
SQL> insert into employee values(1004,"hihi",2002,123); insert into employee values(1004,"hihi",2002,123)
*
第 1 行出现错误:
ORA-00984: 列在此处不允许
三、执行查询语句
1、用单引号正确查出数据。
SQL> select * from employee where job='huhu';
ID NAME EMPNUM JOB
---------- -------------------- ---------- --------------------
1002 you 2001 huhu 2、查询不用单引号将字段类型为char的值引起来报错。 SQL> select * from employee where job=huhu;
select * from employee where job=huhu
*
第 1 行出现错误:
ORA-00904: "HUHU": 标识符无效
3、即使是数字串也不行,必须得加引号。
SQL> select * from employee where job=123;
select * from employee where job=123
*
第 1 行出现错误:
ORA-01722: 无效数字
4、数字串加引号,正确查出
SQL> select * from employee where job='123';
ID NAME EMPNUM JOB
---------- -------------------- ---------- --------------------
1004 hihi 2002 123
1004 hihi 2002 123
四、执行更新操作。
1、set时候赋值用数字串并不加引号,where条件判断时赋值数字串不加引号报错。
SQL> update employee set name=123 where job=123; update employee set name=123 where job=123
*
第 1 行出现错误:
ORA-01722: 无效数字
2、set时候赋值用数字串并不加引号,where条件判断时赋值数字串加引号成功更新。
SQL> update employee set name=123 where job='123';
已更新2行。
3、set时候赋值用非数字串并不加引号,报错。
SQL> update employee set name=wewe where job='123'; update employee set name=wewe where job='123'
*
第 1 行出现错误:
ORA-00904: "WEWE": 标识符无效
4、set时候赋值用非数字串并不加引号,成功更新。
SQL> update employee set name='wewe' where job='123';
已更新2行。