DML運算元據

2021-09-23 13:35:23 字數 3386 閱讀 7293

建立學生表create table student(

stuld int prmary key auto_increment comment學生編號,

stuname varchar(25)comment學生姓名,

stupwd varchar(50)comment學生密碼,

gender char(2)defautcomment性別,

gradeid int comment年級編號編號phone varchar(11comment**,

email varchar(25) comment郵箱,

address varchar(255)defaut位址不詳,

identitycard varchar(18)unque comment省份證號

)comment學生表

1…修改表:

更改identitycard欄位名稱為identityid

change:更改字段資訊時 並且可以改名/modify 只能改欄位資訊

alter table student change identitycard identityid varchar(18);

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

alter table student add borndate datetime comment出生日期

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

alter table student drop borndate;

修改表名

alter table student rename school_stu;

主鍵和外來鍵

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

alter table 表名 add constraint 主鍵名 primary key 表名(主鍵欄位名);

alter table school stu

add constrniat pk_stu_stuld

primary key school stu(stuld)

外來鍵:

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

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

grade主表 student從表

實現物理外來鍵: 通過sql語句將外來鍵繫結好,可保證資料點一致性

alter table school stu

add constraint fk_stu_grade

foreign key (gradeid)

references grade(gradeid);

為了進行資料表操作 在實際專案中一般不設定物理外來鍵 而設定邏輯外來鍵

/*ddl 資料定義語言 create drop …

[dml] 資料操作語言 增刪改

dql 資料查詢語言

dcl 資料控制語言

*/資料新增:tnsert

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

insert into school_stu (stuid,stuname,stupwd) values(2,'王五,『root』);

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

insert into school_stu v省略了字段列表 則需要將所有字段進行賦值 且嚴格按照順序alues(3,『呵呵』,『hehe』,『女』,2,『12345』,『x』,『x』,『***』,『2019-05-23』);

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

insert into school_stu (stuid,stuname) values(null,『我是測試的』);

同時新增多條資料

insert into school_stu (stuname) values(『陳旭』),(『李天一』),(『努力過』);

新增多條資料

insert into school_stu (stuname)

select 『呵呵1』 union

select 『呵呵2』 union

select 『呵呵3』

將school_stu的id和名稱 賦值到stu表中的對應列

必須要保證新增資料的表是提前存在的

insert into stu(stuid,stuname)

select stuid,stuname from school_stu;

將school_stu的id和名稱 賦值到一張新錶newstu中。

create table newstu(

select stuid,stuname from school_stu

);資料修改update

修改語句

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 + 1,phone = 『13838384383』 where stuid = 1;

資料的刪除delete

delete from 表名[where條件]

delete from school_stu where stuname =零零七and gender =;

truncate可進行資料刪除

truncate table school_stu;

**delete與truncate的區別

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

不通電:*delete可進行條件刪除,truncate只能全表刪除。

*delete會保留自增序列,truncate除了表結構,其它資料全部清空

不會保留自增序列。

(truncate會結束事務,而delete不會影響事務

使用DML運算元據

建立學生表 create table student stuid int primary keyauto increment comment 學生編號 stuname varchar 25 comment 學生姓名 stupwd varchar 50 comment 學生密碼 gender char...

使用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 條件不建議,會...