MySQL資料庫基礎 DML資料的增刪改

2021-10-23 19:26:00 字數 1525 閱讀 1140

資料的dml操作:新增資料,修改資料,刪除資料

新增資料

# 格式: insert into 表名[(字段列表)] values(值列表...);

-- 標準新增(指定所有字段,給定所有的值)

mysql> insert into stu(id,name,age,***,classid) values(1,'zhangsan',20,'m','lamp138');

mysql>

--指定部分字段新增值

mysql> insert into stu(name,classid) value('lisi','lamp138');

-- 不指定字段新增值

mysql> insert into stu value(null,'wangwu',21,'w','lamp138');

-- 批量新增值

mysql> insert into stu values

-> (null,'zhaoliu',25,'w','lamp94'),

-> (null,'uu01',26,'m','lamp94'),

-> (null,'uu02',28,'w','lamp92'),

-> (null,'qq02',24,'m','lamp92'),

-> (null,'uu03',32,'m','lamp138'),

-> (null,'qq03',23,'w','lamp94'),

-> (null,'aa',19,'m','lamp138');

修改資料
# 格式:update 表名 set 欄位1=值1,欄位2=值2,欄位n=值n... where 條件

-- 將id為11的age改為35,***改為m值

mysql> update stu set age=35,***='m' where id=11;

-- 將id值為12和14的資料值***改為m,classid改為lamp92

mysql> update stu set ***='m',classid='lamp92' where id=12 or id=14 --等價於下面

mysql> update stu set ***='m',classid='lamp92' where id in(12,14);

刪除資料
# 格式:delete from 表名 [where 條件]

-- 刪除stu表中id值為100的資料

mysql> delete from stu where id=100;

-- 刪除stu表中id值為20到30的資料

mysql> delete from stu where id>=20 and id<=30;

-- 刪除stu表中id值為20到30的資料(等級於上面寫法)

mysql> delete from stu where id between 20 and 30;

-- 刪除stu表中id值大於200的資料

mysql> delete from stu where id>200;

MySql資料庫之DML

mysql資料庫的增刪查改操作 1 插入表中的資料 第一種插入資料的方法 指定所有列 insert into stu number,name age,gender values itcast 0001 zhangsan 28 male 使用第二種方法插入第二組資料,沒有填入的資料的列名,以null填...

資料庫MySQL之DML(三)

1.外來鍵在建立表的時候,增加約束 麻煩,比較複雜 create table grade gradeid int 10 notnull auto increment comment 年級id gradename varchar 50 not null comment 年級名稱 primary key...

DML資料庫操作語言(MySQL)

dml 資料庫操作語言 操作表內內容 一.新增表內容insert insert into 表名 欄位1 欄位2 values 欄位1值1 欄位2值1 欄位1值2 欄位2值2 表內的每個欄位都新增的話可以省略欄位名,比如insert into 表名 values 值 值 二.修改 更新 表內容upda...