SQL Insert Into Value


有兩種方法可以向表中插入值。

第一種方法不需要指定要插入資料的列名,只需要它們的值。

語法

INSERT INTO table_name  
VALUES (value1, value2, value3....);

第二種方法指定要插入的列名稱和值。

語法

INSERT INTO table_name (column1, column2, column3....)  
VALUES (value1, value2, value3.....);

假設有一個學生表,其結構如下所示:

CREATE TABLE STUDENTS (  
    ID INT NOT NULL,  
    NAME VARCHAR (20) NOT NULL,  
    AGE INT NOT NULL,  
    ADDRESS CHAR (25),  
    PRIMARY KEY (ID)  
);

下面要向這個表中插入五條記錄,參考以下語句 -

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (1, 'ABHIRAM', 22, 'ALLAHABAD');  
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (2, 'ALKA', 20, 'GHAZIABAD');  
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (3, 'DISHA', 21, 'VARANASI');  
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (4, 'ESHA', 21, 'DELHI');  
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (5, 'MANMEET', 23, 'JALANDHAR');

不需要指定列名稱,如下所示 -

INSERT INTO STUDENTS 
VALUES (1, 'ABHIRAM', 22, 'ALLAHABAD');  
INSERT INTO STUDENTS
VALUES (2, 'ALKA', 20, 'GHAZIABAD');  
INSERT INTO STUDENTS
VALUES (3, 'DISHA', 21, 'VARANASI');  
INSERT INTO STUDENTS
VALUES (4, 'ESHA', 21, 'DELHI');  
INSERT INTO STUDENTS
VALUES (5, 'MANMEET', 23, 'JALANDHAR');