Day06-mysql

 

database

Access to the database mysql -u用户名 -p密码

Database level

  • Creating a database

create database 数据库的名 character set 字符集 collate 校对规则

  • Delete Database

drop database 数据库名

  • Modify the database

alter database 数据库 character set 字符集(utf8)

  • Query the database

show databases; show create database 数据库的名字 select database();

  • Switching Database

use 数据库的名字

Table level

  • Table creation:

create table table name (column among the constraint of the type column, among the columns of the constraint type column)

Column constraints:

primary key: primary key constraint: The only not empty

PRIMARY (sid, age) primary key: only take effect a

unique: The only constraint: null value may be added a plurality of null, a plurality of tables can be unique.

not null: not null constraint

Automatic growth: auto_increment

default: 'default statement'. The statement here is performed automatically, no need to fill in data

You can also write null.

  • Delete table

drop table 表名

  • Modify table

alter table table name (add, modify, change, drop) rename table the old table to the new table name

original alter table table name rename the new table name

alter table table name character set character set

  • Lookup table structure

show tables; check out all the tables

show create table table name: create table statement, the definition of desc watches table table structure

 Foreign key:

Product table and a foreign key data classification category_cid connected.

Foreign key role : the data associated with multiple tables . Multi-table relationship , the presence of the main table , the table :

 Main Table : Data From Table / primary key ID table where

For example : classification ---- is a source of table data classification name , the primary key CID where the table

Table : Data reference table / table where the foreign key

For example : product table --- cited classification data , the foreign key category_cid where table

Foreign key features :

Table directed from the outer primary key table's primary key

From the data type and length of the key sheet , you must be primary and primary key data type and length  consistent

Declare foreign key constraint: ( is added from a table )

format:

alter table Table add [constraint   foreign key name ] foreign key ( from the outer table key field name ) references the primary table ( the primary table primary key );

 

1, the data type and length of the key sheet , must be primary and primary key data type and length  consistent

 

2, from the primary table and the table is empty , or the data must be complete

 

l Remove the foreign key constraint :( understanding, development of caution) 

 

alter table Table drop foreign key foreign key name

Add-many foreign key Example:

CREATE TABLE actor(
    aid INT PRIMARY KEY,
    NAME VARCHAR(30)
);
CREATE TABLE role(
    rid INT PRIMARY KEY,
    NAME VARCHAR(30)
);
CREATE TABLE actor_role(
arid INT PRIMARY KEY ,
aid INT,
rid INT
)
ALTER TABLE actor_role ADD FOREIGN KEY(aid) REFERENCES actor(aid);
ALTER TABLE actor_role ADD FOREIGN KEY (rid) REFERENCES role(rid);

 

Examples of many foreign key is added:

CREATE TABLE province(
    id INT PRIMARY KEY,
    NAME VARCHAR(20),
    description VARCHAR(20)
);
CREATE TABLE city(
    id INT PRIMARY KEY,
    pid INT ,
    NAME VARCHAR(20),
    description VARCHAR(20)
);
DROP TABLE city ;
ALTER TABLE city ADD FOREIGN KEY (pid) REFERENCES province(id) ;

 

 

 

 

 

 

  • Operation data in the table

Insert: insert into table names (column names, column names) values ​​(value 1, value 2);

Delete: delete from table [where conditions]

Review: update table set column name = 'value' column name = 'value' [WHERE condition];

Query: select [distinct] * [1 column name, column names 2] from table [where conditions]

  • other

as Keywords: back conditions where Alias:

Relational operators:>> = <<= = <!> -

Determine whether a column is empty: is null is not null

in a certain range in between ... and

Logical operators: and or not

Fuzzy query: like _:% represents a single character: represents multiple characters

Group: After filtering group by the grouping condition: HAVING

Aggregation function: sum (), avg (), count (), max (), min () Sort: order by (asc ascending, desc descending)

 

Database backup:

Export: mysqldump -uroot -p password for the target database> f: //aa.sql [Export File Path] is automatically created

Import: mysql -uroot -p password for the target database <f: /sss.sql [path] need to be manually created a database and then imported into the database.

Guess you like

Origin www.cnblogs.com/ych961107/p/11374426.html