Database Introduction
Basic concepts of database
① concept
- English word database: DataBase;
- The database is used to store and manage data warehouses.
② Features
- Persistent storage of data. In fact, the database is a file system
- Easy to store and manage data
- Use a unified approach to operational database - SQL
2. Common database software
- MySQL: free open source database, small database, Oracle has been acquired. MySQL6.x version also start charging. Later, Sun's acquisition of MySQL, Oracle and Sun Microsystems has been acquired
- Oracle: charges of large-scale databases, Oracle's products.
- DB2: IBM's database products and fees. Often used in the banking system.
- SQL Server: database MicroSoft medium-sized companies for a fee. C # ,. net and other languages commonly used.
- SQLite: Embedded small database applications in mobile terminal, such as: Android.
SQL basic operations
1. What is SQL?
- Structured Query Language: Structured Query Language;
- In fact, it defines all the rules of relational database operations.
2. SQL syntax common
- SQL statements can be single or multiple rows of writing, end with a semicolon.
- You can use spaces to indent and enhance the readability of the statement.
- MySQL database SQL statements are not case sensitive, use uppercase keyword suggestions.
- Three kinds of comments
- Single-line comments: - Annotation content annotation content or # (mysql-specific)
- Multi-line comments: / * comment * /
3. SQL classification
① DDL (Data Definition Language) data definition language
- It is used to define the database objects: databases, tables, columns and the like. Keywords: create, drop, alter, etc.
② DML (Data Manipulation Language) data manipulation language
- The data tables in the database used to additions and deletions. Keywords: insert, delete, update, etc.
③ DQL (Data Query Language) data query language
- Used to query records (data) tables in the database. Keywords: select, where, etc.
④ DCL (Data Control Language) database control language (understanding)
- To define the access permissions and security levels, and create user database. Keywords: GRANT, REVOKE, etc.
DDL: operation of the database, table
CRUD 1. Operation of the database
① C (Create): Creating
- Create a database
create database database name; - Create a database, judgment does not exist, then create:
the Create Database not IF EXISTS database name; - Create a database and specify the character set
create database database name of the character set the character set name - The sample code
create database if not exists db1 character set GBK;
② R (Retrieve): inquiry
- All queries the database name:
Show Databases; - Query a database character sets: a database query creation statements
show create database database name;
③ U (Update): modify
- Modify the database character set of
alter database database name of the character set the character set names;
④ D (Delete): Delete
- Delete database
drop database database name; - Judgment database exists, then there is deleted
drop database if exists database name;
⑤ use the database
- Query the database name currently in use
select database (); - Using the database
use the database name;
CRUD 2. Operating data table
① C (Create): Creating
- grammar:
create table 表名(
列名1 数据类型1,
列名2 数据类型2,
....
列名n 数据类型n
);
-
Database type:
-
int: integer type
-
double: decimal type
-
date: date, containing only the date, yyyy-MM-dd
-
datetime: date, time of year, month, day, hour comprising yyyy-MM-dd HH: mm: ss
-
timestamp: the time stamp type comprising Minutes yyyy-MM-dd HH when date: mm: SS
* If this field does not give future assignment, or the assignment is null, the current system time is used by default, automatically assigned -
varchar: String
* name varchar (20): name up to 20 characters
* zhangsan 8 characters seating two characters
-
-
The sample code
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
- Copy the table:
the table name create table table name like to be copied;
② R (Retrieve): inquiry
- The name of a query for all tables in the database
show tables; - Lookup table structure
desc table name;
③ U (Update): modify
- Modify the table name
alter table table name rename to new table name; - Modify the character set table
alter table table name character set the character set name; - Add an
alter table table add Column Name Data Type; - Column Name Type modifications
alter table change table column names new column not new data types;
alter table column name table modify the new data type; - Remove Columns
alter table table name drop column names;
④ D (Delete): Delete
- drop table name;
- drop table if exists 表名 ;
DML: deletions in the data table changes
1. Add data
- grammar
insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
- note
- Column names and values to one correspondence.
- If the table name, column name is not defined, the default value is added to all columns
insert into table values (value 1, value 2, ... n-value); - In addition to digital type, other types require the use of quotation marks (single or double can be) cause to
2. Delete Data
- grammar:
delete from 表名 [where 条件]
- note:
- If not conditions, all records in the table is deleted.
- If you want to delete all records
1. delete from table name; - not recommended. How many records will be executed many times the deletion
2. TRUNCATE TABLE table name; - recommended to use, more efficient to delete the table, and then create a different table.
3. Modify the data
- grammar:
update 表名 set 列名1 = 值1, 列名2 = 值2,... [where 条件];
- note:
- If you do not add any conditions, then all records in the table all the changes.