three main paradigms of database
In order to build a database with small redundancy and reasonable structure, we must follow certain rules when designing database. In a relational database, this rule is called a paradigm. A paradigm is a summary of a design requirement. In order to design a relational database with reasonable structure, a certain paradigm must be met.
The most common design paradigms in real-world development are three:
1. First paradigm (ensuring that each column remains atomic)
The first paradigm is the most basic paradigm. If all the field values in a database table are non-exploded atomic values , the database table satisfies the first paradigm.
the rational compliance of the first paradigm needs to be based on the systemactual Demandto decide. For example, some database systems need to use“Address"This property, which would have been directly"Address"The property is designed as a field on a database table. But if the system often accesses"Address"property in the"City"part, then it is not going to be"Address"This property is re-divided into provinces, cities, detailed addresses and other parts to store, so that in the address of a part of the operation will be very convenient. This design satisfies the first paradigm of the database, as shown in the following table.
The user information shown in the table above follows the requirements of the first paradigm, which makes it very convenient to classify users using cities, and also improves the performance of the database.
2. Second paradigm (ensure that each column in the table is related to the primary key)
The second paradigm is based on the first paradigm in a more advanced layer. The second paradigm needs to ensure that each column in a database table is related to the primary key, not just one part of the primary key (primarily for the Federated primary key). in other words, in a database table, only one data can be saved in a table, and multiple data cannot be saved in the same database table.
For example, to design an order information table, because there may be more than one item in the order, the order number and the product number are used as the federated primary key for the database table, as shown in the following table.
Order Information Form
This creates a problem: The table is the Union primary key with the order number and the product number. In this table, the product name, unit, commodity price and other information is not related to the table's primary key, but only related to the product number. So this violates the design principle of the second paradigm.
And if the Order Information table is split, the product information is separated into another table, the Order Item table is also separated into another table, it is perfect. as shown below.
This design, to a large extent, reduces the redundancy of the database. If you want to get the product information for an order, use the item number to inquire in the product information sheet.
3. Third paradigm (ensure that each column is directly related to the primary key column, not indirectly)
the third paradigm needs to ensure that the data table each column of data is directly related to the primary key and cannot be indirectly related .
For example, when designing an order data table, the customer number can be used as a foreign key and order table to establish the corresponding relationship. Instead of adding fields to the order form about other customer information (such as name, company, etc.). The design shown in the following two tables is a database table that satisfies the third paradigm.
In this way, when the order information is queried, the customer number can be used to refer to the records in the Customer information table, and the data redundancy is reduced without having to enter the contents of the customer information multiple times in the Order Information table.
E-r Chart
e-r - Contact ( entity-relation
composition E-r
· entity type : With a rectangle, the name of the entity is indicated in the rectangle, such as student Zhang Sanfeng, student Li Yu Huan are entities.
· property (Attribute)
· Contact (Relationship) : The diamond-shaped box indicates the contact name and is connected with the relevant entity by the non-forward side, and the type of the contact on the non-Edge side label ( 1:1 , 1:n or m:n ). For example, the teacher to teach students there is a teaching relationship, students choose the course there is the relationship between selection.
as shown in: A class, student, course, teacher ER Figure:
The entity -Contact diagram (entity-relation Diagram) is used to establish the data model, which belongs to the conceptual design stage in the database system introduction, and forms an ER diagram model independent of the machine and independent of the DBMS. It is usually referred to as ER diagram, and the data model depicted by ER diagram can be referred to as ER model accordingly. Er diagrams provide methods for representing entities (that is, data Objects), properties, and connections to describe the conceptual model of the real world.
The ER model, which was first proposed by Peter Chen in 1976, has been widely recognized in the field of database design, but is seldom used as a data model for real database management systems. Even for the SXL-92 database, a well-designed database is challenging. They can be found in many literature on database design, such as the writings of Toby Teorsey (1994).
Most database design products use the entity -Contact model (ER model) to help users with database design. The ER Database design tool provides a "box and arrow" drawing tool to help you create an ER diagram to depict the data.
Edit this paragraph
composition The basic elements of a e-r diagram are entities, attributes, and relationships. A simple example
entity: An entity that objectively exists and can be distinguished from one another. An entity can be either a concrete object or an abstract object.
Property (attribute): An attribute of an entity is called a property. The main attribute (identifier) is a property that uniquely identifies an entity.
Contact (relationship): The interrelationships between entities are called linkages. Contact can be divided into "one-to-one contact", "one-to-many links", "many-to-many links" 3 types.
Edit this paragraph
Components
in the The ER diagram has the following four components:
Rectangle Box: Represents the entity, and the entity name is entered in the box.
Diamond Box: Indicates a contact, and the contact name is entered in the box.
Ellipse Box: A property that represents an entity or contact, and the property name is entered in the box. For the primary property name, underline it under its name.
connections: Between entities and attributes, between entities and relationships, between contacts and attributes, and by a straight line, and by marking the type of contact on a line. (For one-to-one contact, write 1 in the two entity line direction ; For a-to-many connection, write a 1 on a side, write N for many, and write n,m in two entity lines for many-to-many relationships.) )
Unit Test
http://blog.csdn.net/sunzhenhua0608/article/details/8858151
A transaction (Transaction) is the basic unit of concurrency control. The so-called transaction, which is a sequence of operations that are either executed or not executed, is an inseparable unit of work. For example, a bank transfer job: Debit from one account and add another account, both of which are either executed or not executed. Therefore, they should be regarded as a business. A transaction is a unit of data consistency maintained by a database that maintains data consistency at the end of each transaction.
As can be seen from the above description, the proposed transaction is mainly to solve the problem of maintaining data consistency in the concurrency situation.
A transaction has the following 4 basic characteristics.
Atomic (atomicity): The operations contained in a transaction are considered to be a logical unit in which the operations of the logical unit either succeed or fail altogether.
Consistency (consistency): only legitimate data can be written to the database, or the transaction should roll it back to its original state.
Isolation (Isolation): Transactions allow multiple users to concurrently access the same data without destroying the correctness and integrity of the data. At the same time, the modification of parallel transactions must be independent of the modifications of other parallel transactions.
Durability (persistent): After the transaction ends, the result of the transaction must be cured.
2. Statement of the transaction
Start things:BEGIN TRANSACTION
Submit things:COMMIT TRANSACTION
To Roll back a transaction:ROLLBACK TRANSACTION
3.4 properties of a transaction
① atomicity (atomicity): All elements in a transaction are committed or rolled back as a whole, and the elements of a transaction are non-divided, and the transaction is a complete operation.
② Consistency (CONSISTEMCY):when things are done, the data must be consistent, that is, the data in the data store is in a consistent state before the things begin. Ensure that data is lossless.
③ Isolation (Isolation):Multiple transactions that modify data are isolated from each other. This indicates that the transaction must be independent and should not be in any way or affect other transactions.
④ Persistence (Durability):after the transaction is complete, its effect on the system is permanent, and the modification will persist even if a system failure occurs, and the database is actually modified
4. SavePoint of Transactions
Save TRANSACTION savepoint Name--the name and location of the custom save point
ROLLBACK TRANSACTION Save Point Name-rollback to a custom savepoint
Some additions to other masters:
standard definition of a transaction : refers to a series of operations performed as a single logical unit of work, which require four properties of atomicity, consistency, isolation, and persistence, collectively known as ACID properties.
The so-called transaction is a user-defined sequence of database operations , which are either done entirely or not, and are an inseparable unit of work. For example, in a relational database, a transaction can be an SQL statement, a set of SQL statements, or an entire program.
transactions and programs are two concepts. Generally speaking , a program contains multiple transactions.
the start and end of a transaction can be explicitly controlled by the user. If the user does not explicitly define transactions , the DBMS automatically divides things by default
Services. In the SQL language, there are three statements that define a transaction:
BEGIN TRANSACTION
COMMIT
ROLLBACK
Die.
The display transaction is identified with BEGIN TRANSACTION and end transaction, where the update and DELETE statements are either all executed or not executed at all. Such as:
BEGIN TRANSACTION T1
Update student
Set Name= ' Tank '
where id=2006010
Delete from student
where id=2006011
Commit
Simply put, a transaction is a mechanism that maintains the integrity of the database.
The form of implementation is the ordinary SQL statements are embedded in the Begin Tran...commit Tran (or the full form begin Transaction...commit Transaction) and, of course, you can use rollback Tran to rollback a transaction, that is, the undo operation, if necessary.
using the transaction mechanism, the operation of the database is either all executed or not executed, ensuring the consistency of the database. SQL statements that require transactions are typically update and delete operations, and so on.
End Transaction T1
about SavePoint
A user can declare (declare) within a transaction (transaction) called a savepoint (savepoint).
The tag. The save point divides a large transaction into smaller fragments.
The user can use the savepoint (savepoint) to label anywhere within the transaction (transaction)
remember. After the user rolls back the transaction (rolling), it is possible to choose from the current
The execution position is rolled back to any savepoint within the transaction. For example, a user can use a series of complex
A save point is inserted between the new (Update) operation, and if an error occurs during the execution of a statement, use the
The user can roll back to a savepoint before the error without having to resubmit all the statements.
You can also use the Save Point (savepoint) when you develop your application. If a process
(procedure) contains multiple functions (function) that the user can start at each function
Create a save point. When a function fails, It is easy to restore the data to the function before it is executed
Status, rollback (roll back) can modify parameter recall function, or execute related error
Processing.
when a transaction (transaction) is rolled back (rollback) to a savepoint (savepoint),
Oracle releases the locks used by the rollback statement. Other transactions that are waiting for the locked resource can continue
execution. Transactions that require an update (update) of the locked data row can also continue to execute.
The process of rolling back a transaction(transaction) to a savepoint (savepoint) such as
Under
1. Oracle rolls back the statement after the specified save point
2. Oracle retains the specified savepoint, but the save point created after it is cleared
3. Table-level locks (tables Lock) and row-level locks obtained by Oracle when this save point is released (row
Lock), but the previous data lock is still retained.
a partially rolled-back transaction (transaction) is still active and can continue execution.
a transaction (transaction), while waiting for another transaction, rolls back to
a save point (savepoint) operation does not release row lock. To avoid transactions
because a lock cannot be obtained and is suspended, it should be executed Before the UPDATE or DELETE operation, use the for
UPDATE ... The NOWAIT statement. (This is what you get before rolling back the savepoint.)
Lock. The row-level locks obtained after the savepoint are freed, while the save point is executed after the
SQL Statements will also be fully rolled back)
☆ The concept of business
a transaction is a logical set of actions that make up the units of this set of operations, either all succeed or all are unsuccessful.
For example:A--b transfer, corresponding to the following two SQL statements
Update from account set money=money+100 where name= ' B ';
Update from account set money=money-100 where name= ' a ';
☆ Database Open Transaction command
Start Transaction Open transaction
Rollback Rolling back transactions
Commit COMMIT Transaction
Set Transction Isolation Level sets transaction isolation levels
SELECT @ @tx_isolation Query the current transaction isolation level
☆ Characteristics of the transaction (ACID)
atomicity (atomicity) atomicity means that a transaction is an inseparable unit of work, and the operations in the transaction either occur or do not occur.
Consistency (Consistency) The transaction must transform the database from one consistent state to another consistent state.
Isolation (Isolation) Transaction isolation is when multiple users concurrently access the database, the database for each user-opened transactions, can not be disturbed by the operation of other transactions, multiple concurrent transactions to be isolated from each other.
Persistence (Durability) persistence refers to the fact that once a transaction is committed, its changes to the data in the database are permanent, and the subsequent operations and database failures should not have any effect on it.
UseJDBC Management transactions
When a Connection object is created, by defaultJDBC is an autocommit transaction: each time an SQL statement executes, it is automatically committed to the database and cannot be rolled back if the execution succeeds. If you want multiple SQL in the same transaction, you can use the following statement:
☆JDBC Control Transaction Statements
Connection.setautocommit (FALSE);
Connection.rollback ();
Connection.commit ();
☆ Demo Bank Transfer case
in theThe following transfer operations are performed in the JDBC code in the same transaction.
Update from account set money=money-100 where name= ' a ';
Update from account set money=money+100 where name= ' B ';
☆ Set up transaction rollback points
SavePoint sp = Conn.setsavepoint ();
Conn.rollback (SP);
Conn.commit (); Must be submitted after rollback
☆ Isolation level of the transaction
When multiple threads Open data in their transactional operations database, the database system is responsible for isolating operations to ensure that each thread is accurate in obtaining data.
☆ The database defines four isolation levels:
Serializable (serialization): Can avoid dirty reading, non-repeatable reading, the occurrence of false reading.
REPEATABLE READ (Repeat Read): Can avoid dirty read, non-repeatable read the occurrence.
Read Committed: Prevents dirty reads from occurring.
READ UNCOMMITTED (unread): lowest level, none of the above is guaranteed.
☆ The isolation of the transaction
* Dirty read:
refers to a transaction that reads uncommitted data from another transaction.
This is very dangerous, assuming a to B transfer 100 Yuan, a on a computer to open a transaction to perform the following 2 steps, to B deposit 100 yuan, and to reduce their own money 100 Yuan:
1.update account set money=money+100 while name= ' B ';
2.update account set money=money-100 while Name= ' a ';
When the 1th step is finished, the 2nd step has not been executed, a is not submitted, at this time B query his account will find himself more than 100 yuan, thought a turned 100 yuan. If A and B go back, B will lose 100 yuan.
* Non-repeatable reading:
reading a row of data from a table in a single object, multiple read results are different.
For example, the bank wants to query a account balance, the first query a account for 200 yuan, at this time a to account memory of 100 yuan, the bank at this time another query, at this time a account is 300 yuan, the bank may be very confused, do not know which query is accurate. The difference between dirty reads is that dirty reads are dirty data that was not committed by the previous transaction, and non-repeatable reads are re-reading the data that was submitted by the previous transaction.
Many people think this situation is right, need not be confused, of course, the latter will prevail. We can consider such a situation, such as banking procedures need to output the results of the query to the computer screen and write to the file, the result in a transaction for the destination output, the two queries are inconsistent, resulting in inconsistent results in the file and screen, the bank staff do not know which to prevail.
* Virtual Reading
Refers to the data that is inserted into another transaction within a transaction, causing inconsistencies in the read and backward reads.
if the deposit of 100 yuan is not submitted, then the bank to do the report statistics account of all users of the total amount of 2700 yuan, and then C submitted, then the bank again to find accounts of 2800 yuan, resulting in a virtual reading will also make the bank at a loss, in the end to whichever.
Three paradigms of database and its affairs