mysql interview questions (1)

Source Source: https: //www.cnblogs.com/frankielf0921/p/5930743.html

 

1. The primary key, super key, candidate keys, foreign keys

  Primary key: a unique identifier of the table, and has a unique non-empty properties.

  Super Key: in the relationship attribute set can uniquely identify a tuple of the relational model is called super key, it is disguised primary key attributes and belong together super key

  Key called candidate key, referred to here is the set of candidate key is the primary key attribute not containing excess: candidate key

  Foreign key: a key table is another primary key, meaning clear, two tables must have a foreign key, a key table which is the primary key attribute of another table

    Examples to explain:

      There is a student table student (student number, class, age, identity card) and transcripts course (student number, course, score)

         1, and then let's discuss super key, for example where students can watch over key (Student ID), (student number, class), (student number, age, class) contains a unique property here as long as the student number, and ID card It can be called super key

         2, followed by the primary key, the master key with a unique identifier, non-empty, the only attribute, where student number, and ID can be used to identify a person, can be used as a primary key, if you want to use simultaneously two primary keys to identify a things we call composite primary key, here we just set a primary key can find a man, would not have a composite primary key, choose one as the primary key on it

      3, the set of candidate key is the primary key, the above we can ID card or student number defined as the primary key, both here attribute candidate keys, primary keys are selected from the candidate keys inside out, is well understood.  

      4, the foreign key here involves connecting the two tables in the score table, student number of students belonging to the primary key of the table, then the results table Student ID is a foreign key.

2, four characteristics and meanings of things database

  4 features: ACID, atomicity (Atomicity), consistency (Correspondence), isolation (Isolation), persistent (Durability).

  Atomicity : All operations of the whole thing, either completed or not completed all, can not remain in the intermediate links, errors will occur rollback to the state before things began to happen in the course of things, just as this thing is not performed same

  Consistency : Before things start and after the end of things, integrity constraints, things have not been destroyed

  Isolation : isolation transaction execution, so that if they were system unique operations performed within a given time. If there are two transactions running at the same time, perform the same function, isolation transaction will ensure that each transaction in the system that only the transaction using the system. This property is sometimes referred to as serialization, in order to prevent confusion between the transaction operations, or be serialized serialized request, so that only one request at the same time for the same data.

  Persistence : After the transaction is completed, the firm changes made to the database will be stored in the persistent database, and will not be rolled back

3, the role of the view, the view can change it?

  A view is a virtual table, and the table containing the data is not the same, only the view contains a query that retrieves data when using dynamic; does not contain any columns or data. Use view to simplify complex sql operation, specific details are hidden, protected data; create a rear view, may be used in the same manner as the table using them.
  View can not be indexed, it can not have triggers or default values associated with it, if there is order by itself is within view of the order by the view once again will be overwritten.
  Create view: create view XXX as XXXXXXXXXXXXXX;
  for some views, such as not using linkers inquiry packet aggregation function Distinct Union and so on, it can be updated, to update the view of the base table will be updated; but the view is mainly used to simplify retrieval, data protection, is not used to update, and most of the view can not be updated.

Difference 4.drop, delete and truncate the

d ROP deleted directly truncate table to delete data in the table, then insert when id growth since the beginning of delete and from 1 to delete the data in the table, where you can add words.

Process (1) DELETE statement executed is deleted from the table each row deleted, and at the same row as the delete operation for transaction records stored in the log to roll back operations. TRUNCATE TABLE deleted once all the data from the single table does not delete stored records logged, delete rows can not be restored. And does not activate delete trigger associated with a table in the process deleted. Fast execution speed.

(2) table and index space occupied. When the TRUNCATE table, the index table and the space occupied will return to its original size, and DELETE operations without reducing the space occupied by the table or index. drop statement table space occupied by the fully relieved.

(3) In general, drop> truncate> delete

(4) Application. TRUNCATE can only TABLE; DELETE can be a table and view

(5) TRUNCATE and DELETE deletes data only, the entire table is deleted and DROP (and data structure).

(6) truncate delete without a where: Delete only data, without deleting the table structure (as defined) drop statement removes the table structure is dependent constraints (constrain), the trigger (Trigger) index (index); dependent in the table stored procedure / function will be retained, but its status changes: invalid.

(7) delete statement DML (data maintain Language), this operation will be placed in the rollback segment, take effect until the transaction commits. If there is a corresponding tigger, when the execution will be triggered.

(8) truncate, drop a DLL (data define language), operations with immediate effect, the original data is not placed in the rollback segment, you can not roll back

(9) In the absence of a backup, the prudent use of drop and truncate. To remove some rows of data using delete and pay much attention to where the constraints of influence. Rollback segments should be large enough. To delete a table with drop; if you want to retain the table and delete the data in the table, if unrelated to the transaction, with truncate can be realized. If and related matters, or teachers want to trigger trigger, or a delete.

(10) Truncate table name of the table is fast and efficient, because:
TRUNCATE Table and DELETE statement WHERE clause is not functionally the same: both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE speed, and fewer system and transaction log resources used. DELETE statement deletes each row, and each row is a record deleted in the transaction log. TRUNCATE TABLE to delete data stored by the release of the data page table data used, and only release page recorded in the transaction log.

(11) All TRUNCATE TABLE to delete rows in the table, but the table structure and its columns, constraints, indexes and so on remain unchanged. New line count value reseeded identification used for that column. If you want to keep the identity count value, use DELETE. If you want to remove table definition and its data, use the DROP TABLE statement.

(12) to the table referenced by FOREIGN KEY constraint can not be used TRUNCATE TABLE, but should not use the DELETE statement WHERE clause. Because TRUNCATE TABLE is not logged, it can not activate the trigger.

 

5. Database paradigm

A first paradigm (1NF)

at any of a relational database, the first paradigm (1NF) is the basic requirement of the relationship between mode, does not satisfy the first normal form (1NF) database is not a relational database.
The so-called first paradigm (1NF) means for each column of a database table are indivisible basic data items, the same column can not have more than one value, i.e., an entity attribute values or not have a plurality of properties can not be duplicated . If duplicate attribute appears, it may be necessary to define a new entity, the new entity composed of repeating attribute to-many relationship between the new entity and the original entities. Each row in the first paradigm (1NF) table contains only one example of information. In short, the first paradigm is no duplicate column.

2 The second paradigm (2NF)

a second paradigm (2NF) is established on the basis of a first paradigm (1NF) on, i.e., satisfies the second paradigm (2NF) must first normal form (1NF). The second paradigm (2NF) requires each instance or a database table row must only area may be divided. To achieve the distinction typically requires adding a column to the table, to uniquely identify each instance storage. This unique property is called the primary key column or primary key, the main code.
The second paradigm (2NF) required attributes of the entity entirely dependent on the primary key. The so-called completely dependent on the presence of means can not rely solely on a part of the primary key attribute, if present, and this part of the primary key attribute should be separated to form a new entity, between the original and the new entity is an entity of many relationship. To achieve the distinction typically requires adding a column to the table, to uniquely identify each instance storage. Briefly, a second non-primary paradigm is partially dependent on the properties of the non-primary key.

3 a third paradigm (3NF)

third normal (3NF) must satisfy the second paradigm (2NF). Briefly, a third paradigm (3NF) requires a database table comprising a non-primary key information is not already included in other tables. For example, there is a department information table, where each department has a department number (dept_id), department name, department briefings and other information. Then the department number listed in the employee information table after the information can no longer with the department about the department name, department, etc. About adding staff information table. If the department information table does not exist, according to the third normal form (3NF) should build it, otherwise there will be a lot of data redundancy. Briefly, third paradigm is not dependent on other properties of non-primary property. (My understanding is that the elimination of redundancy)

  

Guess you like

Origin www.cnblogs.com/cheneyboon/p/11354942.html