在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教學》
以上是oracle怎麼增加數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!