資料庫的增刪改查

2022-06-07 14:24:08 字數 1873 閱讀 5577

資料的基本操作:增(insert)刪(delete)改(update)查(select);

1.新增資料(insert)

insert into 表名(欄位名1,欄位名2) values (資料1,資料2);

insert into 表名(name,tid) values ('李白','王維');

insert into 表名(name,tid) values (''李白,'王維'),(''杜甫,'孟浩然'); 這是插入多條資料

欄位名和資料要一一對應;

字串和時間日期型別用單引號引起來;

字段可以省略,但是要按順序全欄位資料插入;

2.資料的刪除(delete)

delete from 表名 刪除表中的所有資料但是id不會重置

truncate 表名: 刪除表中所有資料,標識所用值重置;

delete from grade where id=4;  加上限制條件 where可以就指定刪除哪一行

3.修改(update)

update 表名 set 欄位名=新值 [where id = 3];

update grade set name='zhangsan';     這樣寫就把name欄位下面資料全更改了

update grade set name='php' where id =1; 這樣寫是指定name欄位下的id是1的哪一行更改資料

update grade set name='php',title='aaa'  where id =1; 這是修改多條欄位的資訊

4.查詢(select)

select * from 表名;

select *from grade; 這是查詢表中所有欄位下的資料

select 欄位1,欄位2 from 表名

select name,tid from grade;這是查詢欄位name和tid下的資料

select 欄位1 as 『新名字』,欄位2 as 『新名字' from 表名

select name as '姓名' , tid as '學號' from grade;  查詢出來的結果給name和tid起個新名字顯示;

4.1where條件(邏輯and or 比較》、=、<=、<>不等於)

select * from 表名 where 欄位1>值 and 欄位2《值;

(1).where條件中between and   這個其實就是等於 chengji>=10 and chengji<=15;

select * from 表名 where 字段 between 75 and 90;

(2).空條件查詢

select * from 表名 where 字段 is null;(is not null)

select*from grade where chengji is null;

資料庫增刪改查

我們知道當我們的表建立後重複執行會出錯,一般我們會這麼處理 create table if not exists stuinfo 學了新建表我們還應該知道乙個東西,如何刪除表 deop table table name 怎麼檢視別人的見表語句呢 show create table stuinfo 怎...

資料庫增刪改查

import pymysql def getmysqlconn conn pymysql.connect host 172.16.238.130 port 3306,db my mysql user root password 123456 charset utf8 return conn def ...

資料庫增刪改查

資料庫操作 show databases create database 資料庫名 use 資料庫名 select database drop database 資料庫名 資料表操作 create table 表名 欄位名 型別名 約束 show tables drop table 表名 資料表增刪...