Python-MySQL database basics (1) Database basics, introduction and installation of MYSQL, data types

Database introduction

Database (database) referred to as DB is actually a collection of files, a warehouse for storing data, and essentially a file system. The database stores data in a specific format, and users can add, delete, modify, and query the stored data, etc. Actions, music, video, etc. files are also data.

Characteristics of database storage data

  1. Persistent storage, so that data will not be lost due to computer crashes, sudden power failures, etc.
  2. The reading and writing speed is extremely high, and the operation is performed with SQL statements, which is highly efficient.
  3. To ensure the validity of data, there are constraints on data such as data type, data uniqueness, and validity.
  4. It supports programs very well and is easy to expand. It supports databases in languages ​​such as JAVA, PHP, C, and Python.

Classification of database

  1. Relational databases
    can save various relational data in real life. The data stored in the database is in units of tables. Mainstream relational databases: MYSQL, Oracle, SQLServer, etc.
  2. Non-relational databases
    are usually used to solve certain specific needs, such as high concurrent access, mainstream non-relational databases: Redis, Mongodb, memacahe, etc.

MYSQL introduction and installation

Introduction to MYSQL

MYSQL was developed by the Swedish MySQL AB company. It found that the speed and flexibility of using ISAM and mSQL to access tables could not meet the needs, so it developed a data engine MYSQL similar to the mSQL interface. Oracle and SQL Server are commercial databases, and mysql is open source.
MYSQL is an open source relational database management system (RDBMS), which uses the most commonly used database management language (Structured Query Language SQL) for database management. MYSQL has attracted much attention because of its speed, reliability and adaptability. MYSQL is the best choice for managing content when transactional processing is not required. Excel is similar to MYSQL for data management, but the data storage capacity and flexibility are not as good as MYSQL.

MYSQL features

  1. mysql is open source, so you don't need to pay extra to use it.
  2. mysql supports large databases. Can handle large databases with tens of millions of records.
  3. mysql uses the standard SQL data language form.
  4. mysql can be allowed on multiple systems and supports multiple languages. These programming languages ​​include R, Python, Java, PHP, Ruby, etc.

MySQL installation

Download address: http://www.mysql.com/downloads
insert image description here
insert image description here
is divided into online installation and download to local installation. It is selected by default during installation, and then click "Excute", "Next", and "Finish" in turn to solve the problem. After the installation is complete, add the bin folder in the installation directory to the environment variable to use it normally.
Enter the bin folder under the mysql installation directory from cmd, enter mysql -uroot -p, and enter the password to enter the mysql database.
insert image description here
You can also use PhStudy instead of MYSQL. It is very suitable for building websites and is an integrated development environment. After installation, you need to start the MYSQL service to connect to the database.

Graphical interface management tool

When using commands to input, it is a bit inconvenient compared to using excel and other graphical operations. At this time, you need to use a graphical interface management tool, here uses the SQLyong graphical interface.

Introduction to SQLyong

SQLyong is a fast and simple graphical management tool for MYSQL databases, which can effectively manage databases at any location. It is produced by the industry-renowned Webyong company and is also an open source software.
Open source software (open source software), referred to as OSS, open source code software. Therefore, open source software has the characteristics of being free to use and publish the source code.
SQLyong is just a tool to connect to the database, not a database.
The picture below shows the interface after the successful installation of SQLyong. Try not to install it on the C drive. The password is the one used when installing SQL. Do not set it too complicated.
insert image description here
The following figure shows the interface of entering SQLyong
insert image description here
Right-click root@localhost, select Create Database, enter the name of the database as test1, select utf8 as the base character set, select utf8_general_ci as the database collation, and then click Create.
insert image description here
The interface shown in the figure below will appear. Right-click on the table and select Create Table, and the interface for a new table will pop up. Enter the table name as demo1, select InnoDB as the engine, select utf8 as the character set, and enter the column name, data type, and length below. and other information.
insert image description here

MYSQL syntax basics

SQL

SQL is a structured query language, which is a database language used to operate RDBMS (relational database management system). Currently, relational databases support SQL language operations, that is to say, to operate relationships such as orcal, sql server, and mysql through SQL. type database.

Classification of SQL statements

  1. DDL statement: data definition language, these statements define different data points, databases, tables, columns, indexes and other database objects.
  2. DML statements: Data manipulation statements used to add, delete, update, and query database records, and to check data integrity.
  3. DCL statement: a data control statement, which is used to control the direct permission and access level of different data segments.

data integrity

In order to store data more accurately in the table and ensure the correctness and validity of the data, you can add some mandatory validation to the table when creating the table, including the type and constraints of the data fields.

type of data

value type

There are 5 types of integers, and the size of numbers contained in each type is different. The range of defined data is appropriate. When the range of defined data is relatively large, it is recommended to use an unsigned range.

integer type signed range (positive and negative) unsigned range (positive range)
TINYINT(size) -128~127 0~255
SMALLINT(size) -32768~32767 0~65535
MEDIUMINT(size) -8388608~8388607 0-16777215
INT(size) -2147483648~2147483647 0-4294967295
BIGINT(size) -9223372036854775808~9223372036854775807 0~18446744073709551615

There are 3 types of decimals, size refers to the length of the data, and d is the number of decimal places reserved.

decimal type describe
FLOAT(size,d) Decimal numbers with floating decimal points. The maximum number of digits is specified in parentheses, and the maximum number of digits to the right of the decimal point is specified in the d parameter.
DOUBLE(size,d) Large numbers with floating decimal points. Specify the maximum number of digits in parentheses, and specify the maximum number of digits to the right of the decimal point in the d parameter
DECIMAL(size,d) The DOUBLE type stored as a string allows a fixed decimal point. By default, integers are kept and decimals are discarded. (fixed-point type)

string type

string type byte size example
CHAR(size) 0~255 char(3) input 'ab' (add spaces to make up the length of 3 characters), the actual storage is 'ab'; input 'abcd', the actual storage is 'abc'
VARCHAR(size) 0~65535 char(3) input 'ab', the actual storage is 'ab'; input 'abcd', the actual storage is 'abc'

enumerated type

The English of the enumeration type is ENUM, and the enumeration of 1~255 members requires 1 byte of storage; for the 255~65535 members, it needs 2 bytes of storage, and a maximum of 65535 members is allowed. The creation method: enum(" M", "F"), the enumeration value list box will pop up automatically when creating, fill in the content to be input in the input value, such as adding male and female respectively, when entering the table content, you can only enter Or select male, female, enter other values ​​will not be displayed

date type

type of data describe
DATE() Date, format: YYYY-MM-DD, supported range: '1000-01-01' to '9999-12-31'
DATETIME() Combination of date and time, format: YYYY-MM-DD HH:MM:SS, supported range: '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIME() Time, format: HH:MM:SS, supported range: '-838:59:59' to '838:59:59'
YEAR() Year in 2-digit or 4-digit format. 4-digit format: 1901 to 2155; 2-digit format: 70 to 69, indicating from 1970 to 2069
TIMESTAMP() The timestamp changes with the system time and is related to the time zone. Format: YYYY-MM-DD HH:MM:SS, supported range: '1970-01-01 00:00:0' UTC to '2038-01-09 03:14:07' UTC

Notice

  • decimal means a fixed-point decimal. For example, decimal(5,2) means that there are 5 digits in total, and the decimal occupies 2 digits. If it is not specified, it will default to decimal(10,0), and the decimal places will be discarded by default.
  • char represents a fixed-length character string, such as char(3), if filled with 'ab', a space will be added as 'ab'
  • Varchar represents a variable-length string, such as carchar(3), and 'ab' will be stored when 'ab' is filled
  • For pictures, audio, video and other files, they are not stored in the database, but uploaded to a server, and then store the save path of the file in the table
  • Enum types are case insensitive

constraint

  • Primary key constraint (primary key): It can uniquely determine a record in the table, that is, we can make the field not repeated and not empty by adding a constraint to a field. such as ID number
  • Auto-increment constraint (auto_increment), check auto-increment when creating a table, and automatically increase the sequence when filling the content
  • Unique constraint (unique): The value of this field is not allowed to be repeated, the primary key is unique, but the only one is not necessarily the primary key, such as ID cards and bank cards
  • Not Null Constraint (not Null): This field does not allow empty values
  • Default constraint (default): When this value is not filled in, the default value will be used, if it is filled in, the filling will prevail
  • Foreign key constraint (foreign key): Constrains the relational field. When filling in the value for the relational field, it will query whether the value exists in the associated table. If it exists, it will be filled successfully. If it does not exist, it will fail to fill in and throw an exception , the primary key of one table can also be the foreign key of another table

Guess you like

Origin blog.csdn.net/hwwaizs/article/details/128681796