MYSQL the concept Basics

1 Database Overview

1.1 Data Management and development of technology

Database technology is a database administration tasks need to be generated. The mid-1950s before the computer is mainly used for scientific computing. At that time the hardware situation is that only external memory tape, card, tape, no device can directly access the disk and so on; the software situation is, no operating system, no specialized data management software; data processing mode is usually a batch. Data management is accomplished primarily through manpower, we call this stage the data 人工管理阶段. It has the following characteristics at this stage

  • Data is not saved
    because at that time the computer is mainly used for scientific computing, generally do not need to be long-term preservation of data, only one topic in the calculation of the input data, run out will withdraw.
  • Application management data
    needed by the application programmer has a data area to carry out the design, description, definition and management, there is no corresponding software system to be responsible for the management of data.
  • Shared data is not
    data-oriented applications, a set of data corresponding to only one application. When multiple applications are each designed to have a plurality of data definition, can not be used with each other, with reference to each other.
  • Data are not independent
    the physical structure and logical structure data changes, modifications must be made to the application, data is completely dependent on the application, the data lack of independence.

The late 1950s to the mid-1960s, this time has been a hardware disk, drum and other direct access devices; software has been specialized data management software, commonly referred to 文件系统; not only on a batch processing method , but also to achieve the online process. This stage with 文件管理系统the following features management software:

  • Data can be stored for a long
    large number of computers for data processing, data need long-term stay in the external memory perform CRUD operations.
  • Long-term preservation of data from the file system
    using file system management to solve the problem of structured data, but there are still data 共享性差, 数据独立性差的缺点.

Since the late 1960s, the size of the object management computer more and more, more and more wide range of applications, a sharp increase in the amount of data, while a variety of applications, each covering several languages to share data collection requirements have become increasingly demanding. While the hardware has been large-capacity disk, hardware prices drop, software prices. Based on the above background 数据库管理系统came into being.
Compared with manual management phase and the file system, database features mainly in the following areas:

  • Structured data
    structured database system to achieve the overall data, which is the main feature of the database is the essential difference between a database system and file system.
  • Low data sharing high redundancy and ease of expansion
  • High data independence
  • Data organization and management, unified by the database

Then with the development of computer technology, people need to scale data storage and processing of growing, and the internal structure of the object-relational storage of more complex, traditional relational database object-type surface gradually manifested many deficiencies, such as:

  • Machine-oriented data model syntax
    highly structured, only the relationship between discrete data and limited storage of data, semantic representation is weak, the lack of data abstraction.
  • Simple data types is fixed
    , such as: integer, floating point, string, date, etc.; only support a fixed set of characters, which can extend the set of types depending on the application desired.
  • Data Manipulation Language programming language mismatch with
    SQL is a descriptive language, the language programming language is mandatory, not directly use the relational data structures.
  • Limited storage and management of the objects
    lack the ability to represent and manipulate knowledge
    and therefore the gradual emergence of a new generation of technical data, of course, for now the new database technology is not perfect but we now have a number of trends can vaguely guess the future new data technology with the characteristics.
  • It should support data management, object management and knowledge management; must support OO data model.
  • Second generation must be maintained and inherited the art database system: i.e., the second generation of the database system must be maintained during non-data access mode and data independence.
  • We must be open to other open systems database system performance: support database language standard; supports standard network protocols; the system has good portability, connectivity, scalability and interoperability.

Throughout the history of the database we can see the development of databases and applications and related technology development are inseparable
Database development and its relationship diagram of

1.1.2 summary

1.2 Data Model

Database according to the data structure to organize, store and manage data, in fact, a total of three database models: 层次模型, 网状模型, 关系模型.
Hierarchical model
to organize the data by its hierarchy as

Mesh model
data node to the data node is directly connected to each other, so that the overall data structure looks like

Relational model
all the data seen as a two-dimensional table of all of the numbers can be determined by the row and column numbers

1.2 Data Type

1.3 SQL

SQL is the acronym for Structured Query Language It consists of the following parts
DDL: the DEFINE the DATA LANGUAGE
DDL allows users to define data, that is, create table, drop table, modify table structure of these operations (ie common CRUD operations)
DML: Manipulation Language the data
DML provides users add, delete, update data capability, which is the application of daily operation of the database.
DQL: the Data Query Language
DQL: allow users to query data, which is usually the most frequent routine database operations.
DCL: the Data Control Language
DCL: Data Control Language (Data Control Language) in the SQL language, is an instruction for data access control, which can control a specific user accounts on the data table, view tables, stored procedures, user custom control functions and other database objects. GRANT and REVOKE by the two instructions.

1.4 database evolutionary trends

1.5 Relations

表的每一行称为记录(Record),记录是一个逻辑意义上的数据。表的每一列称为字段(Column),同一个表的每一行记录都拥有相同的若干字段。
例如students表的两行记录:

Two rows students table
And recording field description

1.5.1 主键

主键定义:
主键,又称主码(英语:primary keyunique key)。数据库表中对储存数据对象予以唯一和完整标识的数据属性的组合。一个数据表只能有一个主键,且主键的取值不能缺失,即不能为空值(Null)。
但是一般来唯一和完整标识的数据属性的组合可能不止一个,但是也不是说满足这个条件的属性的组合都可以设置为主键。在实际开发和生产的过程中它至少需要满足一个基本原则,即不使用和业务相关的字段作为主键。因为在实际开发和生产的过程中,业务逻辑可能需要频繁修改从而导致与业务逻辑相关的字段也可能需要频繁修改,如果一旦以业务相关字段作为主键,那么该字段的频繁修改会给表的维护和扩展带来极大的障碍,因此在开发过程中我们不使用任何业务相关的字段作为主键,一般情况下则是选择一个和业务逻辑无关的字段作为主键。我们常把这个字段命名为id常见的可作为id字段的类型有:
1.自增整数类型:数据库会在插入数据时自动为每一条记录分配一个自增整数,这样我们就完全不用担心主键重复,也不用自己预先生成主键;
2.全局唯一GUID类型:使用一种全局唯一的字符串作为主键,类似8f55d96b-8acc-4636-8cb8-76bf8abc2f57。GUID算法通过网卡MAC地址、时间戳和随机数保证任意计算机在任意时间生成的字符串都是不同的,大部分编程语言都内置了GUID算法,可以自己预算出主键。

设置主键的操作:

ALTER TABLE students ADD PRIMARY KEY(id)


Of course, the process of building the table can also be provided 主键, and then separately provided with its final results table creating 主键effect is the same.
During construction of the table set both the master key

CREATE TABLE stdudents(
id INT PRIMARY KEY,
class_id INT,
name CHAR(5),
gender CHAR(1),
score DOUBLE
) ;

Delete the primary key:

ALTER TABLE students DROP PRIMARY KEY;

1.5.2 foreign keys

Foreign key definitions:
If the public key is the primary key in a relationship, then this is known as public key of another relation 外键.
Foreign key:

ALTER TABLE students
ADD CONSTRAINT fk_class_id
FOREIGN KEY (class_id)
REFERENCES classes (id);

Remove the foreign key:

ALTER TABLE students
DROP FOREIGN KEY fk_class_id;

1.6 Index

Index definition:
the index is a data structure of a relational database for pre-sorting a column or columns of the plurality of values
increase separate index:

ALTER TABLE students
ADD INDEX idx_score (score);

Increase in multi-column index:

ALTER TABLE students
ADD INDEX idx_name_score (name, score);

Create unique index:

ALTER TABLE students
ADD CONSTRAINT uni_name UNIQUE (name);

Delete the index:

ALTER TABLE students
DROP INDEX idx_name_score (name, score);
或者
DROP INDEX idx_name_score ON students

Guess you like

Origin www.cnblogs.com/goWithHappy/p/mysql1.html