在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怎麼增加資料的詳細內容,更多請關注TW511.COM其它相關文章!