資料的增刪改查

2021-10-08 12:09:43 字數 3084 閱讀 7967

語法:

#insert into beauty(欄位1,欄位2,…)

#values(值1,值2,…);

特點:

1、欄位和值列表必須一一對應

2、字元型和日期型必須用單引號引起來

3、欄位的順序可以和表中字段的順序不一致

4、不可以為null的字段,必須插入值,比如說主鍵id;可以為null的字段,可以不用插入值,使用null來代替,或欄位名和值都不寫

5、欄位和值的個數必須一致

6、字段列表可以省略,預設所有列 ,而且順序和表中的列的順序一致

更新語句 update

語法:update 表名 set 欄位名=新值,欄位名=新值

[where 條件];

刪除的方式一:

#語法:delete from 表 [where 條件]

刪除的方式二:

#語法:truncate table 表名

#二種刪除方式的區別(面試)

1、truncate不能加where條件,而delete可以加where

2、truncate效率較高

3、truncate不能刪除多表,而delete可以

4、刪除帶標識列字段的表時,truncate 刪除後再插入,標識列的值從1開始 # delete刪除後再插入,標識列的值從斷點處開始

5、truncate刪除後,不能回滾;delete刪除可以回滾

1. 執行以下指令碼建立表my_employees

create table my_employees(

id int(10),

first_name varchar(10),

last_name varchar(10),

userid varchar(10),

salary double(10,2)

);create table users(

id int,

userid varchar(10),

department_id int

2.顯示表my_employees的結構

desc my_employees;

3. 向my_employees表中插入下列資料

#id first_name last_name userid salary

#1 patel ralph rpatel 895

#2 dancs betty bdancs 860

#3 biri ben bbiri 1100

#4 newman chad cnewman 750

#5 ropeburn audrey aropebur 1550

#方式一

insert into my_employees values

(1,『patel』,『ralph』,『rpatel』,895),

(2,『dancs』,『ralph』,『rpatel』,895),

(3,『biri』,『ralph』,『rpatel』,895),

(4,『newman』,『ralph』,『rpatel』,895),

(5,『ropeburn』,『ralph』,『rpatel』,895)

#方式二

insert into my_employees

select 100,『patel』,『ralph』,『rpatel』,895 union all

select 100,『patel』,『ralph』,『rpatel』,895 union all

select 100,『patel』,『ralph』,『rpatel』,895 union all

select 100,『patel』,『ralph』,『rpatel』,895

select * from my_employees;

4. 向users表中插入資料

#1 rpatel 10

#2 bdancs 10

#3 bbiri 20

#4 cnewman 30

#5 aropebur 40

insert into users

values

(1,『rpatel』,10),

(2,『bdancs』,10),

(3,『bbiri』,20),

(4,『cnewman』,30),

(5,『aropebur』,40)

5. 將3號員工的last_name修改為「drelxer」

update my_employees

set last_name=『drelxer』

where id = 3;

6. 將所有工資少於900的員工的工資修改為1000

update my_employees set salary=1000

where salary<900;

7. 將userid 為bbiri的user表和my_employees表的記錄全部刪除

delete u,m

from users u,my_employees m

where u.userid=m.userid

and u.userid=『bbiri』;

8. 刪除所有資料

delete from users;

delete from my_employees;

9. 檢查所作的修正

select * from users;

select * from my_employees;

10. 清空表my_employees

truncate table users;

truncate table my_employees;

資料增刪改查

import sqlite3 from db import get db conn,close db conn 測試 db file score.db 插入資料 definset score data 1 獲取連線 conn sqlite3.connect db file 2 開啟游標cursor ...

資料的增刪改查

匯入模組 import pymysql conn pymysql.connect user root db day36 host 127.0.0.1 post 3306,charset utf8 autocommit true 自動提交確認 cursor conn.cursor cursor pym...

Mysql資料增刪改查

建立資料庫表 haha 並寫入字段 create table haha user name varchar 32 age int,sin date date 向字段寫入資料 insert into xx value xixi 12 2016 12 12 insert into xx values w...