MySQL tables, data types, constraints


Database used- link address

One, MySQL commonly used data types

Types of description
int Integer
bigint Long integer
float Numerical
double Numerical
char Fixed-length string
varchar Variable length string
date Date type (year, month, day)
datetime Date type (year, month, day, hour, minute, second, millisecond)
time Date type (hours, minutes and seconds)
BLOB Binary Large Objects (store streaming media information such as pictures and videos)
CLOB Character large object (store larger text, such as 4G string)
  • Any table contains: table structure, table data, index

Two, create to create a table

  • grammar:
create table 表名(字段名1 数据类型(长度) 约束, 字段名2 数据类型(长度) 约束, ...);
drop table if exists course; 
create table course(
	cno varchar(10) COMMENT '课程号',
	cname varchar(20) COMMENT '课程名',
	credit int(11) COMMENT '学分',
	semester int(11) COMMENT '开课学期',
	PRIMARY KEY (cno)
)

Three, INSERT to add data to the table

  • grammar:
insert into 表名 (字段名...) values(1,2, ...)
INSERT INTO course (cno,cname,credit,semester) VALUES ('C001', '高等数学', 4, 1);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C002', '大学英语', 3, 1);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C003', '大学英语', 3, 2);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C004', '计算机文化学', 2, 2);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C005', 'Java', 2, 3);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C006', '数据库基础', 4, 5);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C007', '数据结构', 4, 4);
INSERT INTO course (cno,cname,credit,semester) VALUES ('C008', '计算机网络', 4, 4);

Four, DDL add/delete/modify table structure

  • Use alter table to add/delete/modify the table structure, that is , the table structure changes, but does not affect the data in the table

1. add Add field

alter table course add tel varchar(40);

2. modify modify the field

alter table course modify cname varchar(50);

3. drop delete field

alter table course drop tel;

Five, DML add/delete/modify

  • insert, delete, update, used to add, delete, modify, without changing the table structure

1. insert add table data

grammar:

insert into 表名 (字段名...) values(1,2, ...)
INSERT INTO course (cno,cname,credit,semester) VALUES ('C001', '高等数学', 4, 1);

The number of fields and the number of values ​​are the same, the order is the same, and the data type is the same. The
field name can be omitted, but it is not recommended to omit it because the insert statement will be affected when the field position in the database table changes

2. update modify table data

grammar:

update 表名 set 字段名1=字段值1,字段名2=字段值2, ... where 条件;

Without where conditions, the entire table data is updated

UPDATE course SET cname = '外语' WHERE cno = 'C003';

3. delete delete table data

grammar:

delete from 表名 where 条件;

If there is no where condition, delete all

delete from course where cname = '外语';

4. truncate delete large table data

grammar:

truncate table 表名 where 条件;
  • The table is truncated, only the data is deleted, cannot be rolled back, and is permanently lost
  • When truncate is deleted, the table structure is not deleted, but the data in the deleted table cannot be restored
  • When drop delete, delete the table structure, and delete faster, but the data can be restored

Six, constraints

  • Use constraints to ensure the legality, validity, and integrity of the data in the table

1. Non-empty constraints

  • not null
    set its value to not null for a field , only column-level constraints

2. Unique Constraint

  • unique
    possible to value of a field can not be repeated, can be null, but only a null value
  • The unique constraint can be added after a single field name (column-level constraint), or after two or more field names (table-level constraint), that is, two or more fields cannot be repeated in combination, and you cannot look at any one alone. Field (column-level constraints can also be expressed as table-level constraints)

2.1 NULL discussion

  • Multiple nulls in the unique constraint are not repeated, because null is not a value. In the database, null is not a value, which means there is nothing. It is empty, that is, empty is not a value and cannot be measured by the equal sign. null or is not null
  • ifnull() Null processing function (single-line processing function), that is, data that may be null, what to do

3. Primary key constraints

  • primary key
PRIMARY KEY (字段名)

Primary key column data is unique and non-empty

  • Each table should have a primary key. The primary key can identify the uniqueness of the record. The primary key is divided into a single primary key and a composite (union) primary key . A single primary key is composed of one field, and a composite (union) primary key is composed of multiple fields.
  • The so-called multiple primary keys of a table is called a combined primary key, but the combined primary key can only have one primary key auto_increment constraint. The auto-increment type must be an int type and an int type related type, not a string type or a date type. And so on
    , the self-increment mechanism in Oracle: sequence (sequence)

3.1 The role of the primary key

  • The first paradigm requires that any table should have a primary key. The primary key value is the unique identifier of this row in this table, which can ensure the uniqueness and integrity of the data. At the same time, the primary key retrieval table can increase retrieval speed.

3.2 Classification of primary keys

3.2.1 Divided according to the number of fields in the primary key field

  • Single primary key (recommended)
  • Composite primary key (multiple fields are combined to add a primary key constraint)

3.2.2 Divided according to the nature of the primary key

  • Natural primary key
  • Business primary key (primary key value is linked to the business of the system) (not recommended)

4. Foreign key constraints

  • foreign key
foreign key(子表字段名) references 父表名(父表字段名)

The foreign key of a table if the referenced field is the primary key, the foreign key value must be equal to the primary key value; if the referenced field is not the primary key, it should at least have unique unique constraints, so the foreign key can also be null, but the foreign key is null The data has nothing to do with the associated table, it is the name of the foreign key, so it is generally not used when the foreign key is null

  • Foreign keys are mainly used to maintain the relationship between tables and ensure referential integrity. If a field in the table is a foreign key field, then the value of the field must come from the primary key of the referenced table
  • The referenced table is called the child table, and the referenced table is called the parent table

1. Order requirements

  • Create the parent table before creating the child table
  • When deleting, delete the child table first, and then delete the parent table
  • When adding data, add the parent table first, and then add the child table
  • When deleting data, delete the child table first, and then delete the parent table

5. Check constraints

  • check
    There is a check constraint in the Oracle database, but MySQL does not currently support it

Seven, the designated timing of constraints

1. Specify constraints on the corresponding data columns while
building the table 2. Create the table after the table is built, and add constraints by modifying the table, such as add, modify

Guess you like

Origin blog.csdn.net/LvJzzZ/article/details/109003663