在oracle中,可以利用“INSERT…VALUES”语句来增加数据,语法“INSERT INTO 数据表名 (字段名1,字段名2…) VALUES(数据值1, 数据值2…)”。
本教程操作环境:Windows7系统、Oracle 11g版、Dell G3电脑。
Oracle插入数据(增加数据)
1、创建表格并指定结构
CREATE TABLE DB3.STUINFO( STUID INT, STUNAME VARCHAR(10), SEX INT, AGE INT, CLASSNO VARCHAR(10), STUADDRESS VARCHAR(10), GRADE INT, ENROLDATE DATE, IDNUMBER VARCHAR(20));
2、插入数据
规范用法
INSERT INTO 数据表名 (字段名1,字段名2...) VALUES(数据值1, 数据值2...)
如:
INSERT INTO DB3.STUINFO (STUID, STUNAME, SEX, AGE, CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values ('1', '龙七', '1', 26, 'C201801', '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021992XXXXXXXX');
可以发现数字即便加上引号,Oracle也能智能识别。
当前后一一对应时也可省略table后的列名声明:
INSERT into DB3.STUINFO values (2, '龙八', 1, 25, 'C201801', '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021993XXXXXXXX');
省略列名声明的情况下,前后类型不对应或缺失信息时将报错。
指定列名时,未指定值的列默认填充为null:
INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values (2, '龙八', 1, 25, '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021993XXXXXXXX');
也可在创建表结构的同时为列指定默认值,当无对应输入时自动填充:
CREATE TABLE a ( id INT, name CHAR(10) default 'a', class INT NOT NULL );
INSERT INTO a VALUES (1,NULL,1); INSERT INTO a (id,class) VALUES (1,1);
可得到:
3、可与子查询合用
INSERT INTO a(id,class) VALUES ((SELECT STUID FROM STUINFO WHERE STUNAME='龙七'),2);
STUINFO构建总命令:
CREATE TABLE DB3.STUINFO( STUID INT, STUNAME VARCHAR(10), SEX INT, AGE INT, CLASSNO VARCHAR(10), STUADDRESS VARCHAR(10), GRADE INT, ENROLDATE DATE, IDNUMBER VARCHAR(20)); INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE, CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values ('1', '龙七', '1', 26, 'C201801', '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021992XXXXXXXX'); INSERT into DB3.STUINFO values (2, '龙八', 1, 25, 'C201801', '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021993XXXXXXXX'); INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values (2, '龙八', 1, 25, '厦门市', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021993XXXXXXXX'); INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values (3, '龙九', 2, 23, '三门市', '2017', to_date('01-09-2017', 'dd-mm-yyyy'), '3503041995XXXXXXXX'); INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE,CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values (3, '龙九', 2, 23, 'C201702', '三门市', '2017', to_date('01-09-2017', 'dd-mm-yyyy'), '3503041995XXXXXXXX'); INSERT into DB3.STUINFO (STUID, STUNAME, SEX, AGE,CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER) values (4, '龙十', 2, 23, 'C201702', '三门市', '2017', to_date('01-09-2017', 'dd-mm-yyyy'), '3503041995XXXXXXXX');
推荐教程:《Oracle教程》