資料庫插入 刪除 修改語句

2021-10-10 02:23:47 字數 1700 閱讀 9601

1. 向表中插入一行(該行的每一列都有資料)

insert into 表 values( 值 1 ,值 2)

2.插入資料時,只向某些列插入資料:如果插入的行中有些欄位的值不確定,那麼 insert 的時候不指定那些列即可。

insert into 表 ( 列 1) values( 值 1)

• 自動編號列不需要手動插入。 【set  identity_insert   表名 on 】

• 注意:主鍵不能有重複值。

• 插入資料時的單引號問題。

• insert into 表(列) select 列 1 ,列 2 union  --可一次性插入多行記錄

• insert into 表(列) select 列 1 ,列 2 from 表

• select 列 into 新錶名 from 舊表

• n 字首: n』 字串』,在伺服器上執行的**中(例如在儲存過程和觸發器中)顯示的 unicode 字串常量 必須以大寫字母 n 為字首。即使所引用的列已定義為 unicode 型別,也應如此。如果不使用 n 字首,字串將轉換為資料庫的預設**頁。這可能導致不識別某些字元。

3. 把乙個表中的資料備份到另乙個表中(新錶)

select  *  into  newtable  from  oldtable

4. 把乙個表中的資料備份到另乙個表中(表已經存在)

insert  into  newtable (列1,列2)  select  列1,列2  from  oldtable

5. 修改某列的資料型別

alter table tablename alter column username char(50)

6. 更新資料

• 更新乙個列: update student set  s*** = 『 男』

注意where條件後面的優先順序 not——>and——> or 

7. 刪除資料

• 刪除表中全部資料

delete  from student 。

delete 只是刪除資料,表還在,和 drop table 不同。

• delete 也可以帶 where 子句來刪除一部分資料: delete from student where sage > 20 

• truncatetable student 的作用與 deletefrom student 一樣,都是刪除 student 表中的全部資料,區別在於:

– 1.truncate 語句非常高效。由於 truncate 操作採用按最小方式來記錄日誌,所以效率非常高。對於數百萬條資料使用 truncate 刪除只要幾秒鐘,而使用delete 則可能耗費幾小時。

– 2.truncate 語句會把表中的自動編號重置為預設值。

– 3.truncate 語句不觸發 delete 觸發器。

資料庫語句基礎 資料更改 插入 更新 刪除

1.單行插入 語法格式 insert into 表名 列名表 values 值列表 其中 列名表 中的列名必須是,表名 中有的列名,值列表中的值可以是常量也可以是null,各值之間用逗號分隔 值列表中的值必須與列名表中的列按位置順序對應,他們的資料型別必須相容 insert into student...

資料庫插入 更新 刪除操作

1.插入資料 1 為表中的所有的字段插入資料 insert into 表名稱 欄位1,欄位2,欄位3.vaues 值1,值2,值3.括號內為根據所建立的字段型別逐一進行插入,逐一在給字元型別資料插入的時候,使用單引號 2 為表的指定指端插入資料 insert into 表名稱 指定欄位1,指定欄位2...

插入 刪除 修改表資料

1 插入表資料 insert low priority delayed high priority ignore into tbl name col name,values set col name on duplicate key update col name expr,tbl name 被操作...