MySQL的INSERT命令用于向數據庫表中插入新的數據。它的基本語法如下:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
其中,table_name是要插入數據的表名,column1, column2, ...是要插入數據的列名,value1, value2, ...是對應列的具體值。
以下是一些INSERT命令的具體用法:
INSERT INTO students (name, age, grade)
VALUES ('John', 18, 'A');
上述命令將在students表中插入一條新的記錄,包含name為’John’,age為18,grade為’A’的值。
INSERT INTO students (name, age, grade)
SELECT name, age, grade FROM other_students;
上述命令將從other_students表中選取name、age和grade列的值,并將其插入到students表中。
INSERT INTO students (name, age, grade)
VALUES ('John', 18, 'A'), ('Alice', 19, 'B'), ('Bob', 20, 'C');
上述命令將在students表中插入三條新的記錄,分別包含’John’、18、‘A’、‘Alice’、19、‘B’和’Bob’、20、'C’的值。
INSERT INTO students (name, age)
VALUES ('John', DEFAULT);
上述命令將在students表中插入一條新的記錄,其中age列的值使用默認值。
INSERT INTO students (name, age)
VALUES ('John', 18);
上述命令將在students表中插入一條新的記錄,只設置了name和age列的值,其他列的值將使用默認值。