MySQL-- Data Management

Note: The following operations are based on tables created by the dynamic changes sql statement written renderings can be attached is attached with

1.1, the foreign key

A way, when you create a table, adding constraints (trouble, more complex)

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
-- 定义外键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` DATETIME DEFAULT NULL COMMENT '出生日期',
   `gradeid` INT(10) NOT NULL COMMENT '学生的年级',
   `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

Delete the table of the foreign key relationships, you must first delete references to other people's tables (from the table), then delete the referenced table (the primary table)

Second way: After the success of creating a table, add foreign key constraint

Syntax: ALTER TABLE table ADD CONSTRAINT constraint name FOREIGN KEY (as a foreign key column) REFERENCES that table (which field)

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
-- 定义外键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` DATETIME DEFAULT NULL COMMENT '出生日期',
   `gradeid` INT(10) NOT NULL COMMENT '学生的年级',
   `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`);

The above operations are physical foreign keys, foreign keys in the database level, we do not recommend! (To avoid problems caused by excessive database)

Best Practices:

  • The database is a simple table, just to keep the data, only the row (data) and columns (fields)
  • We want to use data from multiple tables, and want to use the foreign key (program to achieve)

1.2, DML language (all remember)

Database significance: data storage, data management

Language DML: Data Manipulation Language

  • Insert added
  • update modification
  • delete delete

1.2.1, add

insert

grammar:insert into 表名([字段名1,字段2,字段3])values('值1'),('值2'),('值3',....)

1. Insert a field

-- 插入语句(添加)
INSERT INTO `grade`(`gradename`) VALUES('大四')
-- 由于主键自增我们可以省略 (如果不写表的字段,他就会一一匹配)
INSERT INTO `grade` VALUES('大三')  --  Column count doesn't match value count at row 1
-- 所以一般写插入语句,我们一定要数据和字段一一对应!

effect:

2. Insert the plurality of fields

INSERT INTO `student`(`name`) VALUES ('张三')

INSERT INTO `student`(`name`,`pwd`,`sex`) VALUES ('张三','aaaaaa','男')

INSERT INTO `student`(`name`,`pwd`,`sex`) 
VALUES ('李四','aaaaaa','男'),('王五','aaaaaa','男')

effect:

Precautions:

  1. Use between fields and fields separated by commas
  2. Field can be omitted, but the following values ​​must be correspondence, not less
  3. A plurality of data can be inserted at the same time, the VALUES following values, required for use, can be separatedVALUES(),(),....

1.2.2, modify

update

grammar:UPDATE 表名 set colnum_name = value,[colnum_name = value,....] where [条件]

1, modify the name of the participants, with the introduction

UPDATE `student` SET `name`='姜嘉航' WHERE id = 1;

effect:

2, without specifying the conditions will change all the tables!

UPDATE `student` SET `name`='姜嘉航'

effect:

3, a plurality of modified attributes, separated by commas

UPDATE `student` SET `name`='Godles',`email`='[email protected]' WHERE id = 1;

effect:

4, modified in a certain range

UPDATE `student` SET `name`='Godles',`email`='[email protected]' WHERE id < 4;

effect:

We can not know where id is equal to a value greater than a certain value or by case, can be modified within a certain range, the operation returns a Boolean value

5, what the different operators, and the scope of the results

Operators meaning range result
= equal 5=6 false
<> Or! = not equal to 5<>6 true
> more than the
< Less than
<= Less than or equal
>= greater or equal to
BETWEEN … and … 在某个范围内 [2,5]
AND 我和你 && 5>1 and 1>2 false
OR 我或你 || 5>1 or 1>2 true

6、通过多个条件定位数据

语法:UPDATE 表名 set colnum_name = value,[colnum_name = value,....] where [条件]

UPDATE `student` SET `name`='长江7号' WHERE `name`='姜嘉航' AND sex='男'

效果:

注意:

  • colnum_name 是数据库的列,尽量带上``
  • 条件,筛选的条件,如果没有指定,则会修改所有的列
  • value,是一个具体的值,也可以是一个变量
  • 多个设置的属性之间,使用英文逗号隔开

1.2.3、删除

1、delete 命令

语法: delete from 表名 [where 条件]

-- 删除数据 (避免这样写,会全部删除)
DELETE FROM `student`
-- 删除指定数据
DELETE FROM `student`WHERE id = 1;

效果:

2、TRUNCATE 命令

作用:完全清空一个数据库表,表的结构和索引约束不会变!

-- 清空 student 表
TRUNCATE `student`

3、delete 的 TRUNCATE 区别

  • 相同点:都能删除数据,都不会删除表结构
  • 不同:
    • TRUNCATE 重新设置 自增列 计数器会归零
    • TRUNCATE 不会影响事务
-- 测试delete 和 TRUNCATE 区别
CREATE TABLE `test`(
  `id` INT(4) NOT NULL AUTO_INCREMENT,
  `coll` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO `test`(`coll`) VALUES('1'),('2'),('3')

我们可以看一下test表的自动增量,步骤如下:

  1. 右键点击test
  2. 改变表
  3. 右侧选项栏里的高级

我们到这一步看到这个自动增量为:

然后运行:

DELETE FROM `test`

再次打开自动增量看到:

结论:delete不会影响表的自动增量

继续运行:

TRUNCATE TABLE `test` 

再次打开自动增量可以看到:

结论:TRUNCATE会影响表的自动增量,自增会归零!

4、delete删除问题

如果用了delete删除后,重启数据库会发生什么现象?

我们知道MySQL有两个引擎,在两个引擎中用delete发生的现象

  • InnoDB 自增列会重1开始 (存在内存当中的,断电即失)
  • MyISAM 继续从上一个自增量开始 (存在文件中的,不会丢失)

Guess you like

Origin www.cnblogs.com/godles/p/12202847.html