MySQL Advanced Road (two) database operations

Database operations
create a library Books
the CREATE DATABASE IF the NOT EXISTS Books;

Changes to a database character set
ALTER DATABASE books CHARACTER SET gbk;

Library delete
DROP DATABASE books;

Delete library
PROP DATABASE IE EXISTS books;

Operating table
modify table
# ① modify the column name
ALTER TABLE book CHANGE COLUMN publi shdate pubDate DATETIME;

Modify the type of a column or constraint
ALTER TABLE book MODIFY COLUMN pubdate T IME STAMP;
add a new column
ALTER TABLE author ADD COLUMN anrual DOUBLE;

Remove columns
ALTER TABLE author DROP COLUMN a nnua 1 ;
modify table
ALTER TABLE author RENAME TO book_ author;

Delete the table
DROP TABLE book_ author;

Copy only the structure of the table
CREATE TABLE copy LIKE author;

Copy the data structure of the table +
the CREATE TABLE copy2
the SELECT * the FROM author;

Copy only some of the data
the CREATE TABLE COPY3
the SELECT the above mentioned id, au name
the FROM author
the WHERE = Chinese Nation:

Some fields copy only
the CREATE TABLE COPY4
the SELECT ID, name au_
the FROM author
the WHERE 0;

Last_ name column length increased to 50
the ALTER TABLE emp5 the MODIFY the COLUMN Last name VARCHAR (50);

根据表employees创建employees2
CREATE TABLE employees2 LIKE myemployees . employees ;

Table Employees2 rename emp5
the ALTER TABLE Employees2 the RENAME the TO emp5;

How to set unsigned and signed have
the DROP TABLE IEs EXISTS tab_ int;
EREATE TABLE Tab int (
T1 the INT,
T2 UNSIGNE the INT D

Features:
① If you do not set the unsigned or signed, is signed by default, if you want to set an unsigned, unsigned need to add keywords
② If inserted value exceeds the range of integer, an exception will be reported out of range, and insert critical value
③ If the length is not set, the default will be the length

Decimal
Classification:
1. float
a float (M, D)
Double (M, D)

2. The fixed-point
On Dec (M, D)
decimal (M, D)

Features:
M: integer portion of the fractional parts +
D: decimal portion
exceeds the range, the insertion threshold
M and D may be omitted
if a decimal, the default is the M 10, D 0 is the default
of a float and double, it will be in accordance with precision value is inserted to determine the accuracy of
a high accuracy fixed point ③, if the value of the insertion precision demanding operations, such as currency, consider using

Mean cost efficiency characteristics of the M space character written
char char (M) the maximum number of characters may be omitted, the default is a fixed-length character consuming high
maximum number of characters varchar varchar (M), the variable length may not be omitted Compare low character save
the date type
classification:
dATE date only save
time but save time
year only save in
datetime save the date + time
timestamp save the date + time

Constraints
to add timing constraints:
1. Create a table
2. modify the table
constraints Add categories:
column-level constraints: I
the six constraints are supported on grammar, but the foreign key constraint has no effect
table-level constraints:
in addition to non-empty, the default, other support

Six constraint
NOT NULL: non-empty, the guaranteed value for the field can not be null

DEFAULT: The default for this field has a default value to ensure

PRIMARY KEY: primary key value of this field is used to ensure unique, non-null and

UNIQUE: unique key: the guaranteed value for the field having a CD - - resistance, can be null

CHECK: check constraints [mysq1 not supported]

FOREIGN KEY: foreign keys, for limiting the relationship between the two tables for ensuring that the value of this field must be associated with the value from the column of the primary table

See stuinfo all indexes in the table, comprising a primary key, foreign key, unique
SHOW INDEX FROM stuinfo;

Add a column-level constraint

Syntax:
directly in the field names and types can be added behind the type of constraint.
Only support: By default, non-null, primary key, unique

The USE Students.;
The CREATE TABLE stuinfo (
ID the INT a PRIMARY KEY, # primary key
stuName VARCHAR (20) NOT NULL, # nonempty
gender CHAR (1) CHECK (gender = ' M' OR gender = 'F'), # check
seat INT UNIQUE , # unique
age INT dEFAULT 18, # default constraint
majorId INT rEFERENCES major (id) # foreign key
)

Adding table-level constraints

Syntax: at the bottom of the fields
[constraint constraint name] constraint type (field)

CREATE TABLE stuinfo (
id INT,
stuname VARCHAR(20) ,
gender CHAR(1) ,
seat INT,
age INT,
majorid INT,

PRIMARY KEY (id), the primary key
UNIQUE (seat), # unique key
CHECK (gender = * M "ORgender =" F "), # Check
POREIGN KEY (majorid) REFERENCES major ( id) # foreign key
)
SHOW the INDEX the FROM stuinfo;

通用的写法:
JCREATE TABLE IF NOT EXISTS stuinfo (
id INT PRIMARY KEY,
stuname VARCHAR(20) NOT NULL,
sex CHAR(1) ,
age INT DEFAULT 18,
seat INT UNIQUE,
majorid INT,

CONSTRAINT fk_stuinfo major FOREIGN KEY (majorid) REFERENCES major (id)
)

Primary key

DROP TABLE IE EXISTS stuinfo;
CREATE TABLE stuinfo (
id INT,
stuname VARCHAR (20) ,
gender CHAR(1) ,
seat INT ,
age INT ,
majorid INT,
seat2 INT ,
PRIMARY KEY (id, stuname) ,主键
UNIQUE (seat) #唯一键
CHECK (gender =”男” OR gender='女'), #检查
FOREIGN KEY (majorid) REFERENCES major (id)外键
)

Foreign key:
1, requires from the table foreign key relationship
2, association type, and the main table from the foreign key column of the table type column of claim identical or compatible, the name No
3, the associated column of the main table must be a key (typically play a unique primary key)
4, insert the data, first inserted into the master table, from the table and then inserted
, deleted data is removed from the first table, and then delete the main table

Operating constraints
added constraint when modifying table

DROP TABLE IF EXISTS stuinfo;
CREATE TABLE stuinfo (
id INT,
stuname VARCHAR (20) ,
gender CHAR(1) ,
seat INT,
age INT,
majorid INT

# 1. Add-null constraints
the ALTER TABLE stuinfo the MODIFY the COLUMN S tuname VARCHAR (20 is)
the NOT NULL

# 2 add the default constraint
ALTER TABLE stuinfo MODIFY COLUMN age INT DEFAULT 18;

# 3 add a primary key
# ① column-level constraints
the ALTER TABLE stuinfo the MODIFY the COLUMN a PRIMARY KEY the INT ID;
# ② table-level constraints
ALTER TABLE stuinfo ADD PRIMARY KEY (id );

. # 4 the only added
# ① column-level constraints
the ALTER TABLE stuinfo the MODIFY the COLUMN the INT UNIQUE SEAT;
# ② table-level constraints
ALTER TABLE stuinfo ADD UNIQUE (seat) ;

#5.添加外键
ALTER TABLE stuinfo ADD
CONSTRAINT fk. stuinfo _majo2
FOREIGN KEY (majorid) REFERENCES major (id) ;

Delete constraint when modifying tables

# 1 non-null constraint delete
the ALTER TABLE stuinfo the MODIFY the COLUMN stuname VARCHAR (20 is) NULL;
# 2 default constraint delete
the ALTER TABLE stuinfo the COLUMN Age MODIEY the INT;
. #. 3 Drop Primary
ALTER TABLE stuinfo DROP PRIMARY KEY;

# 4 delete the only
ALTER TABLE stuinfo DROP INDEX seat;

# 5 delete foreign keys
ALTER TABLE stuinfo DROP FOREIGN KEY majorid;

Identity column
is also known from the growth column
Meaning: You can not manually insert value, the system provides a default sequence value

# First, create a table identity column set
the CREATE TABLE tab_identity (
ID a PRIMARY KEY the AUTO_INCREMENT the INT,
name VARCHAR (20 is)
);

Column identified by
SET auto_increment_increment = 3;
setting step may be manually inserted by a value set start value

Second, when modifying a table set identity column
ALTER TABLE tab_identity MODIFY COLUMN id INT PRIMARY KEY AUTO INCREMENT;

# Third, delete the identity column when you modify the table
ALTER TABLE tab_identity MODIFY COLUMN id INT PRIMARY KEY;

Guess you like

Origin blog.51cto.com/14509987/2434172