Database
Database Concepts
L Modify one of the data for a series of data
L If I use IO stream, I need to read and write and then find one row to modify.
L or use an array to modify
L This is too complex and inefficient so you need to reference the database
Database essence is a file system that can be seen as a container with good data handling capabilities and an array of collections.
l have a strict data storage format inside.
L Database management System is a software management database
L Common Database management system
- MYSQL Open Source free Swedish company Development Sun Company acquisition Oracle acquisition of Sun Mysql6.x after charging small and medium-sized enterprises use
- Oracle Billing database Oracle Company products for large companies using
- DB2 IBM fees are commonly used in the banking system
- SQL Server Microsoft Corporation charges C #. NET Language Usage
- Sybase has faded out of the historical stage provides a professional data modeling tool PowerDesigner
- SQLite embedded database is used on the mobile phone side
- Java is commonly used for MYSQL and Oracle, and others can also be used
Database and Management Systems
• There can be multiple database databases in MySQL where the data is actually stored
L The relationship between the two
- Client facing database management system
- Database management System Operations database
- Database Operations Table
• MySQL database is a repository of server storage databases
MySQL database has a lot of database MySQL database is a generic term
l need to create your own database to use the database
The data stored in the database must be stored in the table.
Database tables
L store data in tables in a database
L table appears in Java class
L table fields are similar to properties in a class
Every piece of data in a table is similar to an object
L Figure
Table Data
L User Table
L Each table indicates that every thing is a class
Each column represents each property in a class
L Each row is a data representation of every object in a class
MySQL database installation MySQL
- Charges after mysql6.x
- Double-click the 5.5 installation package next
- Choosing a Custom Installation
- Manually select the installation location
- Two installation paths
A) MYSQL Server
b) Server data files
- Click on next to install the
- Click Next again when you are finished installing
- Then tick the lanch ... Run the Configuration Wizard
- Click Finish to do it.
- Start Configuration Wizard
- Select the first fine configuration
- Select the first developer PC
- Select the first multi-function database
- MySQL engine storage location do not change
- Select the third maximum number of custom connections
- Enable TCP/IP network protocol default port number
- Select a third default character set to change to Utf-8
- Install Windows service called MySQL
Import Environment variables
- Set Password
- Click Execute
- Installation Complete
- There is a MySQL service in Windows service that can stop or turn on the service and turn it on automatically or manually
- 360 network connection or the MySQL service program appears
- Command line open Service
MySQL in the system service
MySQL Login
l command line-typed mysql–uroot–p123
L Account number and password
L can also write accounts and passwords separately
L Login Successful
L Bad Password
L Account Error
SQLyog
L Graphical Development tools
L Free Installation
SQL language Overview
Database cannot be in the Java language
L SQL statement to use database to interact with database
L is the code in the database
L is a database query and programming language
L for accessing data queries, updating and managing relational database systems
L CREATE database Create data table add data information to a database requires the use of SQL statements
L applies to all databases
SQL statements
L Data Definition Language DDL
Define database object database tables, columns
Keyword creat alter drop create modify Delete
L Data Manipulation Language DML
Update operations on database data
Keyword Insert Delete update
L Data Control Language DCL
For assigning access and security levels, you can also create users
L Data Query Language DQL
Used to query the records that the database has won
Keyword SELECT from where
SQL General Syntax
The SQL statement can be written as a single line or multiple lines with a semicolon ending
L can use spaces or indents to enhance the readability of the statement
L Case insensitive
L can use/**/to annotate
L Data type
- Int
- Double
- varchar variable character
- Date type is only a month and a day no time or seconds
- There's a lot of other
Database operations
L Create a database
Create Dataabase database name;
Create database name character set character set;
The character set is not specified because the default character set is changed during installation
L Display Database
show databases;
L Delete a database
drop database name;
L Operation No Confirmation
CREATE TABLE Actions
L format
L Constraints
Restrict what data the column names write
- PRIMARY KEY constraint
- Non-null constraint
- Unique constraint
- FOREIGN KEY constraints
Saving of SQL code
L can save normally ctrl+s
l file suffix. sql
L text File
L Import
Ctrl+o
Create a presentation example
L Figure
PRIMARY KEY constraint
L Only non-empty
L do not have any meaning only to identify the life is not the only system has
L Add this keyword after the private key column
L Specify the primary key primary key (ID) at the end of the table
L Auto_increment enables automatic growth of primary keys to ensure uniqueness and non-nullability
L View Table
Show Table name View all tables
DESC Table name View table structure
L Delete a table
DROP table Name
Modify Table Structure
L Add columns
- Alter table name add column name data type constraint
L Modify Columns
- Type modification to note
- Alter table table name modify column name data type constraint
L Modify Column names
- Alter table name change old column name new column name data type constraint
L Delete Columns
- Alter table Name drop column name
L Modify Table name
- Rename table name to new name
L Modify the table character set
Alter table name Character Set character set
Data Table Add data
L Insert into table name (column name 1, column name 2, column name 3) VALUES (value 1, value 2, value 3)
- Note the Column Name Table name problem don't write wrong
- Corresponding problem column and the number of values to correspond
- No string concept characters are enclosed in single quotes '
- Numbers can be without single quotation marks
L Insert into table name (column name) values (value)
- No primary key
- Column names do not write primary key values and do not write primary keys
L Insert into table name values (value)
- All values are given
- Do not write column names
- But the value must be written enough
L Bulk Write
L Insert into table name (column name 1, column name 2, column name 3) VALUES (value 1, value 2, value 3), (value 1, value 2, value 3), (value 1, value 2, value 3),
- Bulk Write Data
- Value Write multiple
If you delete an auto-incrementing ID in the software, add one, and the ID is automatically incremented based on the deleted ID.
Update data
L Update table name set column 1= value 1, column 2= value 2 where condition
- Condition ID
- The wording of the condition
equals = less than < greater than > less than or equal to <=
With or not, and or not
Id in (1,3,4,5) contains
Id not IN (1) does not contain
Delete data
L Delete from table name where condition
- Do not empty one piece of delete does not empty auto_increment record number The next increase will continue to increment on the original basis
L Truncate Table table name destroy tables
- Delete a table directly
- Re-build the table
- Auto_increment will be set to 0 new start
L Query the contents of the table
L MySQL encoding mode is UTF-8 Windows is GBK so will appear Chinese garbled
L Change the encoding of the specified column
L Other
Data query data sheet and test data preparation
Basic Query
L Query the data of the specified column
Select column name 1, column name 2 from table name
L Query All columns
Select * FROM table name
L Remove Duplicate records
Select disctinct column name from table name
L Query and temporarily rename the name of the column
Select column name as ' name to modify ' from table name
Name after add as modified
Or just add the modified name.
L Direct Operations in queries
- Select Column name operator number as ' from table name
- Add as is because the column name changes after the operation.
Conditional query
L SATISFY the condition query does not satisfy the operation
L Comparison Operators
L Logical operators
L query data for a given name
L Greater than
L and
L Between and
L Or
L in
L Like Fuzzy query
- Start with millet
- There are millet at the beginning.
- Query several characters with a few underscores to represent several characters
L The query is not empty
Sort queries
L Ascending
Select * FROM table name order BY column name ASC
ASC can not write
L Descending
Select * FROM table name order BY column name Desc
L QUERY the specified column in descending order
Unable to sort results in Chinese is unpredictable
Aggregation function query (portrait query) concept
L was a horizontal query before.
L sometimes need to calculate a longitudinal query
L need to use an aggregate function to calculate each column and return a single value
Example
L Count
L Sum
L AVG
L Max min
L Mixed calculation
L Group Query
- Grouping repeating content separately for aggregation calculations
- Filter PName
- Filter PName
- Filter sum
Having to filter after grouping
Cannot use where after grouping
Command line garbled problem
The Java self-study road-DAY18