Common operations MySQL database

1. Check all the databases

1
show databases;

2. Create a database encoding format back

1
create database dbName charset= 'utf8' ;

3. Use / switch databases

1
use dbName

4. Check the database being used

1
select  database();

5. Delete Database

1
drop database dbName;  

MySQL data types

    Integer type

Integer type Byte count Sorry, the range of numbers There are in line with the number of ranges
INTYINT 1 0~255 -128~127
SMALLINT 2 0~65535 -32768~32767
MEGIUMINT 3 0~16777215 -8388608~8388607
INT 4 0~4294967295 -2147483648~2147483647
INTEGER 4 0~4294967295 -2147483648~2147483647
BIGINT 8 0~18446744073709551615 -9223372036854775808-9223372036854775807

    Floating-point type

                                      

 

  

    String type

  When common type CHAR and VARCHAR strings, when below their differences

Insert value CHAR(5) The number of bytes occupied VARCHAR(5) The number of bytes occupied
'' '' Five bytes '' A byte
'1' '1' Five bytes '1' Two bytes
'123' '123' Five bytes '123' Four bytes
'123 ' '123 ' Five bytes '123 ' Five bytes
'12345' '12345' Five bytes '12345' Six bytes

   TEXT is a special type of string type. TEXT can only save character data. As news content.

   Types include TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT.

      TEXT from the following 4 types of storage allows the length of the comparison

Types of Allowed length storage
TINYTEXT 0 to 255 bytes Byte length of the value +2
TEXT 0 to 65535 bytes Byte length of the value +2
MEDIUMTEXT 0 ~ 167,772,150 bytes Byte length value +3
LONGTEXT 0 to 4,294,967,295 bytes + 4 byte length of the value

    Date and Time Types

Date Type Byte count Ranges Zero value
YEAR 1 1901~2155 0000
DATE 4 1000-01-01~9999-12-31 0000:00:00
TIME 3 -838:59:59~838:59:59 00:00:00
DATETIME 8 1000-01-01 00:00:00~9999-12-31 23:59:59 0000-00-00 00:00:00
TIMESTAMP 4 19700101080001 00000000000000

    表中常见的操作

1.查看当前数据库中的所有数据表

1
show tables;

2.创建表

create table tablename(字段1 数据类型,字段2 数据类型 ...) [charset set 字符集 collate 校对规则]

3.查看表结构

1
desc tablename;

4.重命名表

1
alter table 表原名 rename to 新表明;

5.添加字段

  添加字段(默认添加在最后一个位置)

   alter table tablename add 字段 数据类型;

  添加字段:在表的第一个位置添加字段

  alter table tablename add 字段数据类型 first;

  添加字段: 在指定的位置添加字段

  alter table tablename add 字段 new 数据类型 after 字段old;

6.修改字段

  修改字段: 修改字段数据类型

  alter table tablename modify 字段 数据类型;

  修改字段: 修改字段到第一个位置

  alter table tablename modify 字段数据类型 first;

  修改字段:修改字段到指定位置

  alter table tablename modify 字段数据类型 after 字段;

  修改字段:只修改字段名称 不修改数据类型

  alter table tablename change 字段 newname 原数据类型;

  修改字段 修改字段名称 同时修改数据类型

  alter table tablename change 字段 newname 新数据类型;  

7.删除字段

  alter table tablename drop 字段;

8.删除表

  drop table tablename;

Guess you like

Origin www.cnblogs.com/xianglei_/p/12072308.html