1. 程式人生 > 實用技巧 >MySQL資料管理

MySQL資料管理

3.MySQL資料管理

3.1外來鍵

方式一:

create table `grade`(
`gradeid` int(10) not null auto_increment comment '年紀id',
`gradename` VARCHAR(50) not null comment '年紀名稱',
PRIMARY key(`gradeid`)
)ENGINE=innodb default CHARSET=utf8

/*
學生表的gradeid要去引用年級表的gradeid
定義外來鍵foreign key
給這個外來鍵新增約束(執行引用) references
*/

create table if not exists `student`(
`id` int(4) not null AUTO_INCREMENT COMMENT '學號',
`name` VARCHAR(30) not null DEFAULT '匿名' COMMENT '姓名',
`pwd` varchar(20) not null default '123456' COMMENT '密碼',
`sex` VARCHAR(2) not null DEFAULT '男' COMMENT '性別',
`birthday` TIMESTAMP DEFAULT null COMMENT '出生日期',
`gradeid` int(10) not null COMMENT '年級id',
`address` VARCHAR(100) DEFAULT null COMMENT '地址',
`email` VARCHAR(50) DEFAULT null COMMENT '郵箱',
PRIMARY KEY(`id`),
key `FK_gradeid`(`gradeid`),
CONSTRAINT `FK_gradeid` FOREIGN key (`gradeid`) REFERENCES `grade`(`gradeid`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

刪除有外來鍵關係的表的時候,必須先刪除從表,後刪除主表

方式二

create table `grade`(
`gradeid` int(10) not null auto_increment comment '年紀id',
`gradename` VARCHAR(50) not null comment '年紀名稱',
PRIMARY key(`gradeid`)
)ENGINE=innodb default CHARSET=utf8

/*
學生表的gradeid要去引用年級表的gradeid
定義外來鍵foreign key
給這個外來鍵新增約束(執行引用) references
*/

create table if not exists `student`(
`id` int(4) not null AUTO_INCREMENT COMMENT '學號',
`name` VARCHAR(30) not null DEFAULT '匿名' COMMENT '姓名',
`pwd` varchar(20) not null default '123456' COMMENT '密碼',
`sex` VARCHAR(2) not null DEFAULT '男' COMMENT '性別',
`birthday` TIMESTAMP DEFAULT null COMMENT '出生日期',
`gradeid` int(10) not null COMMENT '年級id',
`address` VARCHAR(100) DEFAULT null COMMENT '地址',
`email` VARCHAR(50) DEFAULT null COMMENT '郵箱',
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

--建立表的時候沒有外來鍵關係
alter table `student`
add CONSTRAINT `FK_gradeid` FOREIGN key(`gradeid`) REFERENCES `grade`(`gradeid`);

/*
alter table 表名 add constraint 約束名 foreign key(作為外來鍵的列)references 那個表(哪個欄位)
*/

3.2DML語言(資料庫操作語言)

*Insert

*update

*delete

3.2.1新增(insert)

--插入語句
--insert into 表名([欄位1][欄位1][欄位1]...[欄位n])values(`值1`),(`值2`),(`值3`)...(`值n`)
insert into `grade`(`gradename`)VALUES('大三') --資料和欄位一一對應

注意事項:

1、所有的符號為英文符號

2、欄位可以省略,但是後面插入的值要一一對應

3、可以同時插入多條資料

3.2.2修改(update)

/*
update 修改條件 set原來的值 = 新值
修改多個屬性,逗號隔開
*/
--修改語句
--update 表名 set 屬性=替換屬性值 where (條件)主鍵 = 主鍵值;
update `student` set `name`='Rose' where id = 1;

條件:

where 子句 運算子 id = 某個值,大於某個值,或者在某個區間範圍

操作符含義範圍結果
= 等於 5=6 false
!= 不等於 5!=6 true
> 大於 5>6 false
< 小於 5<6 true
>= 大於等於 5>=5 true
<= 小於等於 6<=5 false
between...and... 在什麼什麼之間 between 2 and 5 在2-5之間查詢值
AND && 5>1 and 1>2 false
OR || 5>1 or 1>2 true

注意點:

-條件,如果沒有指定則會修改所有的列

-value,是一個具體的值,也可以是一個變數

-英文符號注意

3.2.3刪除(delete)

/*
刪除資料
delete from 表名 where 主鍵 = 主鍵值;
*/
delete from `student` where id = 1;

TRUNCATE命令:

作用:完全清空一個數據庫表,表結構和索引約束不變

--清空student表
truncate `student`;

相同點:都能刪除資料,都不會刪除表結構

不同點:

-TRUNCATE 重新設定 自增列 計數器迴歸零

-TRUNCATE 不會影響事務


--TRUNCATE he delete 的區別
create table `test`(
`id` int(4) not null auto_increment,
`coll` varchar(10) not null,
primary key(`id`)
)ENGINE = INNODB DEFAULT CHARSET = utf8

insert into `test`(`coll`)values('1'),('2'),('3')

delete from `test`

truncate table `test`