mysql simple addition, deletion and modification


1. About the use of mysql addition, deletion and modification

1) Increase:

-- 在表内添加元素
insert into biao values(2,'a');

2) Delete:

-- 删除某一行元素
delete from biao where id=2;
-- 可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
delete from biao ;
-- 删除某一个字段
alter table biao drop column classes;

3) Modify:

-- 把修改某一列的元素  (把某一列的元素全部清空相当于把这一列的所以元素改为null)
update biao set classes= null;
-- 修改某一行的元素 set后面写需要修改哪一个字段的内容 ,where后面写修改的是哪一行
-- 当符合判断条件的元素不止一个的时候  就会把每一个符合条件的元素都修改了
update biao set classes= 'kkk' where id=3;

2. About building a table

1) Basic table format

create table cc(
	student_id int  comment '学号',
	student_name varchar(10)comment '姓名',
	age int default 18 comment '年龄',
	sex char  comment '性别',
	phone int(12)  comment '电话',
	address varchar(50)  comment '地址',
	class_id int(5) 
)

create table table name (
student_id (field name) int (type) coomment'student number' (remarks)
)

2) Constraints

What are constraints?
Constraints are some rules defined in the table used to maintain the integrity of the data database.

By defining the constraint dao for the columns in the table, you can prevent the wrong data from being inserted into the table, and you can also maintain the consistency of the data between the tables

. If a constraint only acts on a single column, it can be defined as a column constraint or a table constraint;

If a constraint condition scopes multiple columns, it must be defined as a table constraint.

1. NOT NULL non-empty constraint
1.1)NOT NULL 约束强制列不接受 NULL 值。

1.2)NOT NULL 约束强制字段始终包含值。这意味着,如果不向字段添加值,就无法插入新记录或者更新记录。
2. UNIQUE unique constraint
2.1)UNIQUE 约束唯一标识数据库表中的每条记录。

2.2)UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。

2.3)PRIMARY KEY 拥有自动定义的 UNIQUE 约束。

2.4)请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
3. PRIMARY KEY primary key constraint
3.1)PRIMARY KEY 约束唯一标识数据库表中的每条记录。

3.2)主键必须包含唯一的值。

3.3)主键列不能包含 NULL 值。

3.4)每个表都应该有一个主键,并且每个表只能有一个主键。
4. FOREIGN KEY foreign key constraints
4.1)一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。
5. CHECK check constraints
5.1)CHECK 约束用于限制列中的值的范围。

5.2)如果对单个列定义 CHECK 约束,那么该列只允许特定的值。

5.3)如果对一个表定义 CHECK 约束,那么此约束会在特定的列中对值进行限制。
6.DEFAULT default value
6.1)DEFAULT 约束用于向列中插入默认值。

6.2)如果没有规定其他的值,那么会将默认值添加到所有的新记录。

Guess you like

Origin blog.csdn.net/JL_Java/article/details/109221667