Mysql common Tutorial

 

A database:

Create a database create databases database name ;

Delete the database drop databases database name ;

Display Database show databases;

 

Operation of the two database tables

1 syntax to create tables create table table name

{

Field name 1 , Data Type [ constraints ]

Field Name 2 , data type [ constraints ]

Field name 3 , the data type [ constraints ]

Field name 4 , a data type [ constraints ]

Field name 5 , data type [ constraints ]

 

....................................................

Field name n, the data type [ constraints ]

}

supplement:

MySQL data types

 

Five basic types

An integer type: the BIT , BOOL , TINY the INT , the SMALL the INT , MEDIUM, the INT , the INT , the BIG the INT;

2 Float Type: FLOAT , DOUBLE , DECIMAL ;

3 String Type: CHAR , VARCHAR , TINY the TEXT , the TEXT , MEDIUM, the TEXT , LONGTEXT , TINY the BLOB , the BLOB , MEDIUM, the BLOB , a LONG the BLOB ;

4 date type: a Date , DateTime , TimeStamp , Time , Year ;

5 other data types: BINARY , VARBINARY , the ENUM , the SET , the Geometry , Point , MultiPoint , the LineString , a MultiLineString , Polygon , the GeometryCollection and the like .

 

② create a common statement

  the Create the Table tb_dept (      

  Id int Primary Key AUTO_INCREMENT, # department number plastic primary key growth since 

  the Name VARCHAR ( 18), # department name 

  the Description VARCHAR ( 100) # Description 12); 

 

  the Create the Table tb_emp ( 

   the above mentioned id int Primary Key AUTO_INCREMENT, # AUTO_INCREMENT just MySQL specific 

  the Name VARCHAR ( 18 is ), 

  Sex VARCHAR ( 2 ), 

  Age int, 

  address VARCHAR ( 200 is ), 

  in Email VARCHAR ( 100 ) 

  );

 

 

Common operating the three-Database

 

# Delete table

drop table tb_dept;

# View information table

desc tb_dept;

# Note: It is not possible under any circumstances to modify, # only can be modified only when the field contains only null values.

alter table tb_emp modify sex  varchar(4);

# Increase Column

Alter table tb_emp add tel varchar(4);

# Remove Columns

Alter table tb_emp drop tel varchar(4);

Alter table tb_emp drop column tel;

 

# Column renamed

 

Alter table tb_emp change Name emp_Name varchar(18);

 

# Table renamed

Alter table tb_emp rename emp;

Rename table emp to tb_emp;

 

# Restraint

/*

Not null non-empty;

Unique unique key

Primary key primary key

Check inspection

*/

 

# Example 
Create Table tb_emp ( ID int Primary Key AUTO_INCREMENT, the Name VARCHAR (
18 is ), Sex VARCHAR ( 2) default ' M ' check (Sex = ' M ' or Sex = ' F ' ), # table level written check in mysql ineffective Age int, address VARCHAR ( 200 is ), In Email VARCHAR ( 100 ) UNIQUE, the dept_id int, # References tb_dept (ID) # foreign key has no writing table level constraint foreign key fk_emp (the dept_id) References tb_dept (ID) ) ;

 

Guess you like

Origin www.cnblogs.com/changfan/p/11230199.html