有两种方法可以向表中插入值。
第一种方法不需要指定要插入数据的列名,只需要它们的值。
语法
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');