mysql新增語句 Mysql中插入資料語句

2021-10-25 14:18:42 字數 1727 閱讀 4569

在mysql中insert into、insert into ... set ...、insert ignore into、replace into都是插入資料的語句。

insert into 的 sql語法:

新增一行資料(如果資料時字元型,需要加上單/雙引號)

insert into table_name (field1,field2,...)

values (value1,value2,...);

這種方式是指定字段新增對應的資料。

新增一行資料(給當前表中所有欄位都加上資料)

insert into table_name values (value1,...)

這裡需要給所有欄位都加上資料(id主鍵自增可以使用null代替)。

insert into 可以插入相同重複的資料,而insert ignore into不允許插入相同的重複資料。

insert into ... set ...的 sql 語法:

insert into ... set ... 作為 insert into語法的擴充套件插入一行資料。(該語法在php中有時使用起來比較方便)

insert into table_name set field=value,field=value...

使用舉例:

insert into test set `name`='hello',`age`=20;

insert ignore into 的 sql語法:

insert ignore into 和 insert into的用法一致。insert ignore會忽略資料庫中已經存在的資料,如果資料庫沒有資料,就插入新的資料,如果有資料的話就跳過這條資料。

insert into test(`name`,`age`) values('nihao',29);

-> query ok, 1 row affected (0.00 sec)

insert ignore into test(`name`,`age`) values('nihao',29);

-> query ok, 0 row affected (0.00 sec)

insert ignore into當插入資料時,在設定了記錄的唯一性後,如果插入重複資料,將不返回錯誤,只以警告形式返回。

replace into 的 sql語法:

repalce的執行和insert很相似。replace參考

如果存在primary 或 unique相同的記錄,則先刪除掉。再插入新記錄。(假如表中的乙個舊記錄與乙個用於primary key或乙個unique索引的新記錄具有相同的值,則在新記錄被插入之前,舊記錄被刪除。 )

除非表有乙個primary key或unique索引,否則,使用乙個replace語句沒有意義。

replace 的三種形式:repalce參看文件

1.replace into table_name (field,...) values(value,...)

2.replace into table_name (filed,...) select ...

3.replace into table_name set field=value,...

replace into test (`name`,`age`) values ('ni',25);

replace into test (`name`,`age`) select 'ss',20

replace into test set `name`='us',`age`=24

mysql語句新增索引

參考 mysql索引學習 2 建立索引 修改索引 刪除索引的命令語句 mysql語句新增索引 建立或新增索引可以使用如下語句。一 使用alter table語句建立索引。語法如下 1.primary key 主鍵索引 mysql alter table table name add primary ...

刪除mysql主鍵語句 MySQL主鍵新增 刪除

2改動資料庫和表的字符集 alter database maildb default character set utf8 改動資料庫的字符集 alter table mailtable default character set utf8 改動表的字符集 假設您想要把錶預設的字符集和全部字元列 c...

Mysql 語句刪除新增主鍵

一 刪除主鍵 1 如果主鍵id不是自動遞增 alter table tb drop primary key 刪除主建 2 如果主鍵id是自動遞增 alter table tb change id id int 10 刪除自增長 alter table tb drop primary key 刪除主建...