MySQL (a) - installation, create a database table, DML language

1. Briefly

MySQL is a relational database management system , a relational database to store data in separate tables rather than putting all the data in a large warehouse, thus increasing the speed and improved flexibility.

Database (Database) is in accordance with the data structure to organize, store and manage data warehouse . Each database has one or more different API (Application Programming Interface, Application Programming Interface) is used to create, access, manage, search, and copy the saved data. We also can be in the file, but read and write data in a file relatively slow data storage. So now we use a relational database management system (RDBMS) to store and manage large amounts of data.

Relational database is built on the relational model based on the database, by means of a set of algebraic and other mathematical concepts and methods to process data in the database.

Relational database management system (RDBMS) features

  • Data is presented in tabular form
  • Each behavior of various recording name
  • Each column name corresponding to the recorded data field
  • Many rows and columns of a form
  • Some form composition database

RDBMS term

  • Database: The database is a collection of related tables.
  • Data: Table are matrix data. In a database table looks like a simple spreadsheet.
  • Column: one (data element) contain the same type of data, such as a zip code.
  • Rows: one (= tuple or record) is a group of related data, such as a user subscription data.
  • Redundancy: store twice as much data redundancy reduces the performance, but to improve the security of the data.
  • Primary key: Primary key is unique. A data table can contain only one primary key. You can use the primary key to query the data.
  • Foreign Key: foreign key for associating the two tables.
  • Composite key: a composite key (key combination) the index key as a plurality of columns, typically used in the composite index.
  • Index: using the index can quickly access specific information in a database table. Index value in the database is one or more columns in the table is sorted a configuration. Catalog of books similar.
  • Referential integrity: the integrity of the relationship is not allowed in reference to the requirements referenced entity does not exist. Integrity is the entity relationship model integrity constraints that must be met, the purpose is to ensure data consistency.

Classification database
relational database: (SQL)

  • MySQL,Oracle,Sql Server,DB2, SQLlite
  • Data stored by the relationship between the rows and columns between the tables, such as: student information table, time sheets, ......

Non-relational databases: (NoSQL) Not Only

  • Redis, MongDB
  • Non-relational databases, object storage, determined by the properties of the object itself.

2. Installation Tutorial

  • Installing MySQL
    Tutorial: https: //www.cnblogs.com/hellokuangshen/p/10242958.html
  • Install SQLyog
    ① Double-click the exe file to install Sqlyog
    ② registered
    ③ Open the database connection
    Here Insert Picture Description
    ④ create a new database School
    Here Insert Picture Description
    ⑤ a new table student
    Here Insert Picture Description
  • The command-line connection to the database connection
    Here Insert Picture Description

3. Operation Database

Database Operation> The operation of the database tables> table operation data database
mysql keywords case-insensitive

The basic operation of the database commands 3.1

(1) Create a database
CREATE DATABASE [IF NOT EXISTS] database name;
(2) delete the database
DROP DATABASE [IF EXISTS] database name;
(3) the use of the database
above the tab key, if your table or column name is a special character, we need to bring ``
the USE 数据库名;
(4) View all databases
SHOW dATABASES

3.2 database column type

(1) Numerical

  • tinyint very small data one byte
  • smallint 2 bytes smaller data
  • mediumint medium-sized data of the three bytes
  • Int integer of four bytes common standard of int
  • bigint larger data 8 bytes
  • Float float 4 bytes
  • double floating point number 8 bytes (accuracy problems!)
  • Float financial calculations decimal string, the general is the use of decimal

(2) String

  • char string of fixed size of 0 to 255
  • varchar string variable from 0 to 65,535 common variable String
  • micro text tinytext 2 ^ 8--1
  • text text string 2 ^ 16-1 save large text

(3) The date and time

  • date YYYY-MM-DD, date format
  • time HH: mm: ss time format
  • datetime YYYY-MM-DD HH: mm: ss format most commonly used time
  • timestamp timestamp 1970.1.1 to the current number of milliseconds! Also more commonly used!
  • year representing the year

(4)null

  • No value unknown
  • Be careful not to use the NULL operation, the result is NULL

3.3 database field attributes (focus)

(1)Unsigned

  • Unsigned integer
  • A statement that the column can not be declared negative

(2)zerofifill

  • 0 filled
  • Insufficient number of bits used to fill the 0, int (3), 5 - 005

(3) increment

  • Generally understood incremented automatically on the basis of a record of the + 1 (default)
  • Is generally used to design unique primary key ~ index, type must be an integer
  • It can be custom-designed by the primary key from the start and step

(4) non-empty NUll not null

  • Assumptions set to not null, if not assigned to it, the error will be!
  • NUll, if you do not fill in the value, the default is null!

(5) Default

  • Set the default value!
  • sex, male default value, if the value of this column is not specified, there will be a default value!

3.4 Creating a database table (focus)

  • Create a table student1
    Here Insert Picture Description
  • Format
    the CREATE TABLE [the IF the NOT EXISTS] 表名(
    'field name' Column type [attributes] [index] [Note],
    'field name' Column type [attributes] [index] [Note], ...
    'field name' Column type [Properties ] [index] [Note]
    ) [table type] [character set] [Notes]

Table 3.5 Data Type

  • About database engine
    INNODB default , high security, transaction processing, multi-table multi-user operating
    MYISAM early years of use, space-saving, faster
    Here Insert Picture Description
  • Physical space exists in a position of
    all the database files are present data directory under a folder corresponding to a database to
    store files or nature
  • MySQL engine differences in physical file
    InnoDB is only one table in the database file * .frm, and the parent directory files ibdata1
    MYISAM the corresponding file
    * .frm table structure definition file
    * .MYD data file (Data)
    * Index .MYI file (index)
  • Setting up the database table character set encoding
    CHARSET = utf8
    is not set, will be mysql default character set encoding ~ (does not support Chinese!)
    MySQL default encoding is Latin1, does not support Chinese
    configure the default encoding in my.ini in
    character- set-server = utf8

3.6 Modify Delete table

(1) modify the table: ALTER TABLE table name RENAME AS old new table name
EG: ALTER TABLE RENAME AS teacher1 Teacher
(2) increasing the field table: ALTER TABLE table name ADD ranked attribute field []
EG: ALTER TABLE ADD teacher1 age INT (11)
field (3) changes to the table (renamed, modified constraint!): ALTER tABLE table name field ranked attribute mODIFY []
EG: the ALTER tABLE mODIFY teacher1 Age VARCHAR (. 11)
(. 4) field of the same name: ALTER tABLE table name CHANGE old name new name column attribute []
EG: ALTER tABLE teacher1 CHANGE Age AGE1 INT (1)
field (5) to delete tables: ALTER tABLE table DROP field name
EG: ALTER tABLE teacher1 DROP AGE1
(6) delete the table: If the table exists and then delete
eg: DROP tABLE IF eXISTS teacher1

Note :

  • All try to create and delete operations plus judgment , in order to avoid error ~
  • `` Field name , use this package!
  • Single-line comments - - Multi-line comments / / **
  • sql keywords are not case sensitive, I suggest that you write lowercase
  • All symbols for all in English !

4. MySQL data management

4.1 foreign key

MySQL in the same "key" and "index" is defined, the same as the foreign key and primary key index is one. The difference is that MySQL will automatically for the primary key index of all tables, but the foreign key field must be clearly indexed by the user . Field for foreign key relationship must be explicitly indexed in all of the reference table, InnoDB does not automatically create an index.
A foreign key can be one to one , a record table is only connected to a record of another table, or one to many , a plurality of recording another table connection table.
Advantage is: You can make two tables linked to ensure data consistency and to achieve some cascading operation. Data consistency, integrity, the main purpose of the control data stored in the external key table. Association is formed so that two tables, the foreign key column only reference values in the look! It may be such that two tables associated, to ensure data consistency and to achieve some cascading operation.
Method 1: When creating a table, adding constraints (trouble, more complex)
Here Insert Picture Description
Here Insert Picture Description
delete the table of the foreign key relationships, you must first delete references someone else's table, and then delete the table (the primary table) referenced (from the table)

Second way: Create a table after a successful, add a foreign key constraint
Here Insert Picture Description
above operations are physical foreign keys, foreign keys in the database level, we do not recommend!

  • Best Practices
    database is a simple table, just to keep the data, only the row (data) and columns (fields)
    we want to use data from multiple tables, and want to use the foreign key (program to achieve)

4.2 DML language (all remember)

Database significance: data storage, data management
DML language: data manipulation language , through which users can achieve the basic operation of the database, DML operation refers to data table records of operations, including adding table records (insert), modified ( update), delete (the delete)
(1) Add insert

Syntax: insert into table ([1 field names, field 2, field 3]) values ​​( 'value 1'), ( 'value 2'), ( 'value 3', ...)

  • Exercise
    Here Insert Picture Description
    Here Insert Picture Description

Precautions :

  • Use between fields and fields separated by commas
  • Field can be omitted, but the following values ​​must be correspondence, not less
  • A plurality of data can be inserted at the same time, the VALUES following values, need to be used to partition the VALUES (), (), ...

(2) modify the update

Syntax: UPDATE table set colnum_name = value, [colnum_name = value, ...] where [condition]

  • Exercise 1
    Here Insert Picture Description

  • English IIHere Insert Picture Description

  • Exercise three
    Here Insert Picture Description

  • Exercise four
    Here Insert Picture Description

  • Conditions: WHERE clause operator id is equal to a value greater than a value, modified in a certain range. ...
    operator returns a Boolean value
    Here Insert Picture Description

  • Note:
    colnum_name a column of the database, `` possible wear
    conditions, filter conditions, if not specified, all the columns will be modified
    value, which is a specific value, may be a variable
    between a plurality of set property, comma separated

(3) Delete

Method 1: delete command
syntax: delete from table [where conditions]

  • Exercise
    before deleting
    Here Insert Picture Description
    After deleting
    Here Insert Picture Description

Second way: TRUNCAT command
syntax: TRUNCATE TABLE table name
role: a completely empty database tables, indexes and constraints table structure will not change!

  • TRUNCATE difference delete the
    ① same point: can delete data, delete the table structure will not
    ② different:
    TRUNCATE will reset the auto-increment counter to zero
    TRUNCATE not affect the affairs
    ③ delete the test and the difference between TRUNCATE
    DELETE the FROM test- delete data after Inserting data will not affect the increment
    step one

    Here Insert Picture Description
    step two
    Here Insert Picture Descriptionstep three
    Here Insert Picture Description
    TRUNCATE TABLE test1- insert data from deleted data after zero growth will be
    step one
    Here Insert Picture Description
    step two
    Here Insert Picture Description
    step three
    Here Insert Picture Description

Learn to: DELETE deletion problem, restart the database, the phenomenon of
InnoDB auto-increment will be re 1 start (in memory of them, power that is lost)
MyISAM continue on a self-increment from the beginning (there is file, will not be lost )

Published 62 original articles · won praise 2 · Views 2739

Guess you like

Origin blog.csdn.net/nzzynl95_/article/details/104075486