Oracle Database -primary key / foreign key relationships and references

Summary describes my personal primary key (primary key), foreign key (foreign key), key candidate (Candidate key), Super key (super key), references the

concept:

Primary Key: the user selects a candidate tuple of key identification, the primary key NOT NULL

Foreign keys: two tables to describe the relationship, the foreign key can be null

Ultra bond: a set of attributes that uniquely identifies the tuple

Containing no extra super key properties: a candidate key

Example:

If students and teachers have the following two tables:

Student(student_no,student_name,student_age,student_sex,student_credit,teacher_no)

Teacher(teacher_no,teacher_name,teacher_salary)

Super key: Student table according to student number (student_no), or ID number (student_credit), or (student number, name) (student_no, student_name), or (student number, ID number) (student_no, student_credit), etc. to uniquely determine which one student, these combinations can be used as super key of this table

Candidate keys: keys are super candidate key, is the smallest and super key, if a removing any attribute key combination is no longer super-super-key. Student tables candidate key as student number (student_no), ID number (student_credit)

Primary key: primary key is a candidate key can be a human decision will normally select a number as a primary key for the table. Now were selected student_no, teacher_no Student table as the primary key, Teacher table

Foreign Key: teacher_no two public key table, and the table is the primary key Teacher and Student so teacher_no is a foreign key table, and the table used to describe the relationship Student table Teacher

 

--References usage

Creating a Student table:

Create table Student(

student_no number(10) not null,

student_name varchar2(10) not null,

student_age number(4) not null,

student_sex varchar2(4) not null,

student_credit  varchar2(18) not null,

teacher_no number(10) not null,

constraint PK_Student primary key (student_no) - set the primary key

);

 

Creating a Teacher table:

Create table Teacher(

teacher_no number(10) not null,

teacher_name varchar2(10) not null,

teacher_salary number(10) not null,

constraint PK_Teacher primary key (teacher_no) - set the primary key

);

- Create foreign key constraint

alter table Student add constraint FK_Student_References_Teacher (teacher_no) references Teacher(teacher_no);

 

1.primary key

☆ If a table has a primary key, then the primary key of the value it can not be null, and each record can not be repeated (identical), otherwise the following error occurs

A. When the primary key is set to null: ERROR 1048 (23000): Column 'id' can not be null

B. When the primary key repeat: ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

例子:create table t2 ( id int(4) not null primary key, --auto_increment, name char(20) not null, sex int(4) not null default '0', degree double(16,2));

A. sql statement: insert t2 values ​​(1, 'www', 1,99.8); when given two passes

B. sql statement: insert t2 values ​​(null, 'www', 1,99.8);

Results: select * from t2;

+----+--------+------+----------+
| id   | name | sex | degree |
+----+--------+------+----------+
|  1   | www  |   1    |  99.80  |
+----+--------+------+----------+

Otherwise, when no primary key table

语句:create table t1 ( id int(4), name char(20));

C.insert t1 values ​​(1, 'www'); can be performed n times

D.insert t1 values ​​(null, 'www'); may be executed n times

The results: select * from t1;

+--------+---------+
| id        | name |
+--------+---------+
|    1       | www  |
|    1       | www  |
| NULL | www  |
+--------+--------+

2.foreign key

Conditions have three foreign keys

① two tables must be InnoDB tables, MyISAM does not support foreign key table
     foreign key columns must be established ② index, after MySQL 4.1.2 version is automatically created when you create a foreign key index, but if an earlier version of the We need to explicitly established;
     ③ two columns of the table must be a foreign key relationship similar data types, i.e. types of interchangeable columns, such as int tinyint and may not be the int and char;

We build a table: create table t1 (id int (4), name char (20)) type = innodb; index and create index t1_index on t1 (name);

Then build a table: create table t3 (id int (4), name char (20), foreign key (name) references t1 (name) type = innodb);

那么 insert t3 values(1,'aaa');就会报错:ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`anwei`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`name`) REFERENCES `t1` (`name`))

However, complete insert t1 values ​​(1, 'aaa'); after it, i.e. values ​​(1, 'aaa'); is not given, but the other value then being given on the turn.

Description: name is permissible null value, the null value is not so limited.
---------------------

Create a field, the field name is first, followed by field data type, this field is constrained behind, it is generally not null, unsigned, auto_increment, default, and so on.

Then one by one to explain the concept of the subject in five

unique key: in a table may identify one or more columns, so both can be considered as column-level constraints, it can be seen as table-level constraints. A column intended to limit all of the elements are not repeated column element can be null.
primary key: in a table can have only one, to restrain the column all the elements will not be repeated, and can not be null.
Two or more contact differences: primary = unique + not null argument is a desirable; Primary is an index, when the system is to be a record in a lookup table may be marked by a primary key column, to quickly find the desired record , so here primary modified columns are generally well defined in advance, it is used for the column index, such as: id int unsigned not null auto_increment; this column. The unique key is only used to limit the list of elements is not repeated, not necessarily used to quickly retrieve, for example: we want to ensure that each record in a table "ID" field does not repeat or "telephone number" field in each record is not repeated , you can be constrained with a unique, at a time when such a column with an index lookup does not matter.
foreign key: a table of foreign key points to unique key in another table (of course, can also be a primary key, and generally should be the primary key, after all, primary key not null value). Is a table, the column identified Foreign, wherein each element, the element can be mapped to a column in another table of the unique identifier.
index / key: Both generally equate treated equally. Its function: If you are identified as the primary key field, like, can use the index to quickly get the results of the query how to do? a primary key can only set a table, then you can use the index / key to Lie Jiasuo lead to a field, thus greatly accelerating the speed of the lookup column element.
---------------------

 

trunc function for intercepting Date Time

Usage: trunc (field names, precision)

Specific examples:

In table1 table, there is a field called SYSDATE, the line id = 123, date: 2016/10/28 15:11:58

1, when the time to intercept years, sql statement is as follows:

select trunc (sysdate, 'yyyy') from table1 where id = 123; --yyyy also be replaced year

Display: 2016/1/1

2, time to intercept month, sql statement:

select trunc(sysdate,'mm') from table1 where id=123;

Display: 2016/10/1

3, when the interception time to date, sql statement:

select trunc(sysdate,'dd') from table1 where id=123;

Display: 2016/10/28

4, when the time taken to hours, sql statement:

select trunc(sysdate,'hh') from table1 where id=123;

Display: 2016/10/28 15:00:00

5, when the time taken to minutes, sql statement:

select trunc(sysdate,'mi') from table1 where id=123;

Display: 2016/10/28 15:11:00

6, the time taken to do not know how to operate temporarily seconds

7, not directly by trunc (sysdate, 'yyyy-mm-dd'), will prompt "excessive precision specifier character"

 

 

Summary describes my personal primary key (primary key), foreign key (foreign key), key candidate (Candidate key), Super key (super key), references the

concept:

Primary Key: the user selects a candidate tuple of key identification, the primary key NOT NULL

Foreign keys: two tables to describe the relationship, the foreign key can be null

Ultra bond: a set of attributes that uniquely identifies the tuple

Containing no extra super key properties: a candidate key

Example:

If students and teachers have the following two tables:

Student(student_no,student_name,student_age,student_sex,student_credit,teacher_no)

Teacher(teacher_no,teacher_name,teacher_salary)

Super key: Student table according to student number (student_no), or ID number (student_credit), or (student number, name) (student_no, student_name), or (student number, ID number) (student_no, student_credit), etc. to uniquely determine which one student, these combinations can be used as super key of this table

Candidate keys: keys are super candidate key, is the smallest and super key, if a removing any attribute key combination is no longer super-super-key. Student tables candidate key as student number (student_no), ID number (student_credit)

Primary key: primary key is a candidate key can be a human decision will normally select a number as a primary key for the table. Now were selected student_no, teacher_no Student table as the primary key, Teacher table

Foreign Key: teacher_no two public key table, and the table is the primary key Teacher and Student so teacher_no is a foreign key table, and the table used to describe the relationship Student table Teacher

 

--References usage

Creating a Student table:

Create table Student(

student_no number(10) not null,

student_name varchar2(10) not null,

student_age number(4) not null,

student_sex varchar2(4) not null,

student_credit  varchar2(18) not null,

teacher_no number(10) not null,

constraint PK_Student primary key (student_no) - set the primary key

);

 

Creating a Teacher table:

Create table Teacher(

teacher_no number(10) not null,

teacher_name varchar2(10) not null,

teacher_salary number(10) not null,

constraint PK_Teacher primary key (teacher_no) - set the primary key

);

- Create foreign key constraint

alter table Student add constraint FK_Student_References_Teacher (teacher_no) references Teacher(teacher_no);

 

1.primary key

☆ If a table has a primary key, then the primary key of the value it can not be null, and each record can not be repeated (identical), otherwise the following error occurs

A. When the primary key is set to null: ERROR 1048 (23000): Column 'id' can not be null

B. When the primary key repeat: ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

例子:create table t2 ( id int(4) not null primary key, --auto_increment, name char(20) not null, sex int(4) not null default '0', degree double(16,2));

A. sql statement: insert t2 values ​​(1, 'www', 1,99.8); when given two passes

B. sql statement: insert t2 values ​​(null, 'www', 1,99.8);

Results: select * from t2;

+----+--------+------+----------+
| id   | name | sex | degree |
+----+--------+------+----------+
|  1   | www  |   1    |  99.80  |
+----+--------+------+----------+

Otherwise, when no primary key table

语句:create table t1 ( id int(4), name char(20));

C.insert t1 values ​​(1, 'www'); can be performed n times

D.insert t1 values ​​(null, 'www'); may be executed n times

The results: select * from t1;

+--------+---------+
| id        | name |
+--------+---------+
|    1       | www  |
|    1       | www  |
| NULL | www  |
+--------+--------+

2.foreign key

Conditions have three foreign keys

① two tables must be InnoDB tables, MyISAM does not support foreign key table
     foreign key columns must be established ② index, after MySQL 4.1.2 version is automatically created when you create a foreign key index, but if an earlier version of the We need to explicitly established;
     ③ two columns of the table must be a foreign key relationship similar data types, i.e. types of interchangeable columns, such as int tinyint and may not be the int and char;

We build a table: create table t1 (id int (4), name char (20)) type = innodb; index and create index t1_index on t1 (name);

Then build a table: create table t3 (id int (4), name char (20), foreign key (name) references t1 (name) type = innodb);

那么 insert t3 values(1,'aaa');就会报错:ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`anwei`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`name`) REFERENCES `t1` (`name`))

However, complete insert t1 values ​​(1, 'aaa'); after it, i.e. values ​​(1, 'aaa'); is not given, but the other value then being given on the turn.

Description: name is permissible null value, the null value is not so limited.
---------------------

Create a field, the field name is first, followed by field data type, this field is constrained behind, it is generally not null, unsigned, auto_increment, default, and so on.

Then one by one to explain the concept of the subject in five

unique key: in a table may identify one or more columns, so both can be considered as column-level constraints, it can be seen as table-level constraints. A column intended to limit all of the elements are not repeated column element can be null.
primary key: in a table can have only one, to restrain the column all the elements will not be repeated, and can not be null.
Two or more contact differences: primary = unique + not null argument is a desirable; Primary is an index, when the system is to be a record in a lookup table may be marked by a primary key column, to quickly find the desired record , so here primary modified columns are generally well defined in advance, it is used for the column index, such as: id int unsigned not null auto_increment; this column. The unique key is only used to limit the list of elements is not repeated, not necessarily used to quickly retrieve, for example: we want to ensure that each record in a table "ID" field does not repeat or "telephone number" field in each record is not repeated , you can be constrained with a unique, at a time when such a column with an index lookup does not matter.
foreign key: a table of foreign key points to unique key in another table (of course, can also be a primary key, and generally should be the primary key, after all, primary key not null value). Is a table, the column identified Foreign, wherein each element, the element can be mapped to a column in another table of the unique identifier.
index / key: Both generally equate treated equally. Its function: If you are identified as the primary key field, like, can use the index to quickly get the results of the query how to do? a primary key can only set a table, then you can use the index / key to Lie Jiasuo lead to a field, thus greatly accelerating the speed of the lookup column element.
---------------------

 

trunc function for intercepting Date Time

Usage: trunc (field names, precision)

Specific examples:

In table1 table, there is a field called SYSDATE, the line id = 123, date: 2016/10/28 15:11:58

1, when the time to intercept years, sql statement is as follows:

select trunc (sysdate, 'yyyy') from table1 where id = 123; --yyyy also be replaced year

Display: 2016/1/1

2, time to intercept month, sql statement:

select trunc(sysdate,'mm') from table1 where id=123;

Display: 2016/10/1

3, when the interception time to date, sql statement:

select trunc(sysdate,'dd') from table1 where id=123;

Display: 2016/10/28

4, when the time taken to hours, sql statement:

select trunc(sysdate,'hh') from table1 where id=123;

Display: 2016/10/28 15:00:00

5, when the time taken to minutes, sql statement:

select trunc(sysdate,'mi') from table1 where id=123;

Display: 2016/10/28 15:11:00

6, the time taken to do not know how to operate temporarily seconds

7, not directly by trunc (sysdate, 'yyyy-mm-dd'), will prompt "excessive precision specifier character"

 

Guess you like

Origin www.cnblogs.com/klb561/p/11080431.html