使用DML運算元據

2021-09-24 02:34:54 字數 3382 閱讀 8479

–建立學生表

create

table

`student`

( stuid int

primary

keyauto_increment

comment

'學生編號'

, stuname varchar(25

)comment

'學生姓名'

, stupwd varchar(50

)comment

'學生密碼'

, gender char(2

)default

'男'comment

'性別'

, gradeid int

comment

'年級編號'

, phone varchar(11

)comment

'**'

, email varchar(25

)comment

'郵箱'

, address varchar

(255

)default

'位址不詳'

comment

'位址'

, identitycard varchar(18

)unique

comment

'身份證號'

)comment

'學生表'

;

1.修改表

1.1更改identitycard欄位名稱為identityid

change:更改字段資訊時 並且可以改名/

modify 只能改欄位資訊

alter

table student change identitycard identityid varchar(18

);

1.2新增字段資訊 出生日期birthday/borndate datetime comment 『出生日期』

alter

table student add borndate datetime

comment

'出生日期'

;

1.3刪除字段資訊 刪除出生日期

alter

table student drop brondate;

1.4修改表名

alter

table student rename school_stu;

主鍵和外來鍵(面試題)

主鍵:能夠唯一標識資訊表中的一條資料的字段/欄位組

外來鍵(面試題)

外來鍵:在一張表中的某個字段引用的資料來自另一張表的某個欄位的資訊。

主表:它的主鍵一般是需要被從表引用 從表:在其中擁有外來鍵 可以引用主表的主鍵資訊

insert into 表名[(要新增值的字段列表)] values(字段值列表《必須按照前面的順序賦值》);

insert

into school_stu (stuid,stuname,stupwd)

values(2

,'小明'

,'root'

);

如果你省略了字段列表 則需要將所有字段進行賦值 且嚴格按照順序

insert

into school_stu values(3

,'呵呵'

,'hehe'

,'女',2

,'12312'

,'x'

,'x'

,'***'

,'2019-05-23'

);

如果有些欄位有預設(主鍵有自動遞增) 則可以使用null來表示 / 你不需要去給此字段賦值

insert

into school_stu (stuid,stuname)

values

(null

,'我是測試的'

);

同時新增多條資料

insert

into school_stu(stuname)

values

('小李'),

('小明'),

('小格'

);

update 表名 set 欄位名 = 字段值,… [where條件語句]

如果不新增條件 則預設為全表更新

update school_stu set stuname =

'李天二'

where 在哪/**?

# where 後可接一系列的判斷條件 id = *** and *** = xx or *** = xx and not ***

update school_stu set stuname =

'李易峰'

where stuid =7;

update school_stu set stuname =

'李元霸'

where gender =

'男'

年級公升級

update school_stu set gradeid = gradeid +

1,phone =

'13838384383'

where stuid =

1;

delete

from 表名 [

where 條件]

delete

from school_stu where stuname =

'李元霸'

and gender =

'男';

truncate 可以進行資料刪除

truncate

table school_stu;

面試題:

相同點:都可以進行資料刪除(全表刪除)

不同點:delete可以進行條件刪除 truncate只能進行全表刪除

不同點:delete會保留自增序列 truncate除了表結構 其他資料全部清空 不會保留自增序列

#不同點(先了解):truncate會結束事務 而delete不會影響到事務

DML運算元據

建立學生表create table student stuld int prmary key auto increment comment學生編號,stuname varchar 25 comment學生姓名,stupwd varchar 50 comment學生密碼,gender char 2 d...

使用DML運算元據庫

create table student stuid int primary key auto increment comment 學生編號 stuname varchar 25 comment 學生姓名 stupwd varchar 50 comment 學生密碼 gender char 2 de...

DML運算元據 天冷加衣

二 刪除資料 三 修改資料 insert into 表名 列名1 列名2,列名n values 值1,值2,值n 簡化 insert into 表名 values 值1,值2,值n 需要寫全所有列的值 除了數字,其他型別值需要加引號 單雙都可 delete from 表名 where 條件不建議,會...