The MySQL database

MySQL database (on)

MySQL data types: numeric types, date and time, strings

1, numeric type

NOTE: BIT stored bit data type field value, and supports MyISAM, MEMORY, InnoDB and BDB tables

Types of size Range (signed) Range (unsigned) use
TINYINT 1 byte (-128,127) (0,255) Small integer values
SMALLINT 2 bytes (-32 768,32 767) (0,65 535) Large integer values
MEDIUMINT 3 bytes (-8 388 608,8 388 607) (0,16 777 215) Large integer values
INT or INTEGER 4 bytes (-2 147 483 648,2 147 483 647) (0,4 294 967 295) Large integer values
BIGINT 8 bytes (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) Maximum integer value
FLOAT 4 bytes (E + -3402 823 466 38 494 351 -1175 E-38) 0, (E-1175 494 351 823 466 351 E 38,3.402 + 38) 0 (E-1175 494 351 823 466 38,3.402 E + 38) Single-precision
floating-point value
DOUBLE 8 bytes (-1,797 693 134 862 315 7 E + 308, -2 225 073 858 507 201 308 E-4) 0 (2,225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) 0 (2,225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) Double-precision
floating-point values
DECIMAL Of DECIMAL (M, D), if M> D, M + 2 is otherwise D + 2 Depending on the value of M and D Depending on the value of M and D Small value

2, the date and time types

Note: Each time type has a value range of valid values ​​and a "zero" value of "zero" when the value of the specified unlawful MySQL can not be represented.

Types of Size
(bytes)
range format use
DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD Date values
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS Time value or duration
YEAR 1 1901/2155 YYYY Year Value
DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS Date and time value mixing
TIMESTAMP 4

1970-01-01 00:00:00/2038

End times are 2147483647 seconds, the Beijing 2038-1-19 11:14:07 GMT, January 19, 2038 early morning 03:14:07

YYYYMMDD HHMMSS Mixing date and time value, the time stamp

 3, character string type

Note: 1, BINARY and VARBINARY similar to CHAR and VARCHAR, except that they contain binary strings rather than non-binary strings. That is, they contain byte strings rather than character strings . This is that they have no character set , and sorting and comparison is based on column values numeric value byte . BINARY keyword is case sensitive.

Types of size use
CHAR 0-255 bytes Fixed-length string
VARCHAR 0-65535 bytes Variable-length strings
TINYBLOB 0-255 bytes No more than 255 characters in binary string
BLOB 0-65535 bytes Long text data in binary form
MEDIUMBLOB Bytes 0-16777215 Binary text data in the form of medium length
LONGBLOB 0-4294967295 bytes Great text data in binary form
TINYTEXT 0-255 bytes Short text strings
TEXT 0-65535 bytes Long text data
MEDIUMTEXT 0-16777215 bytes, Medium length text data
LONGTEXT 0-4294967295 bytes Great text data

MySQL create data tables, query data table data for the table to sort the data in the data

First, the structure of the table:

1, the data table: table name, the table column (column), row (database record), index (Index is a table, which holds the primary key index field, and point recording entity table), the default value ( DEFAULT)

2、表中的列(column)字段名(column_name)和字段属性(column_type):数据类型、“索引”(primary keyunique、index)、AI(自增,具有AI属性的列必须要建索引且该列的值必须是唯一的)、“约束”(BIN-二进制数据、UN-无符号数据、ZF-zero fill 填充0)

二、数据表操作指令

0、创建和选择数据库(database)

mysql> CREATE DATABASE RUNOOB;
mysql> use RUNOOB;

 1、创建指令,(在MySQL控制台,; 是命令终止符 -> 是换行符标识,enter键换行

mysql> CREATE TABLE runoob_tbl(
   -> runoob_id INT NOT NULL AUTO_INCREMENT,
   -> runoob_title VARCHAR(100) NOT NULL,
   -> runoob_author VARCHAR(40) NOT NULL,
   -> submission_date DATE,
   -> PRIMARY KEY ( runoob_id )
   -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;

2、查看数据表指令

# 查看数据库中所有的数据表 
mysql>show tables;
# 查看数据库中表名为stations的数据表 
mysql>desc stations;

3、删除数据表

# 删除数据库中的数据表 
mysql>DROP TABLE runoob_tbl;

4、数据表中的数据发生变化

4.1、向数据表中特定的列插入数据

mysql> INSERT INTO runoob_tbl 
    -> (runoob_title, runoob_author, submission_date)
    -> VALUES
    -> ("学习 PHP", "菜鸟教程", NOW());

4.2、更新数据表中的数据

UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

4.3、删除数据表中的数据

DELETE FROM table_name [WHERE Clause]

5、select查询数据表数据

5.1、读取数据表信息

mysql> select * from runoob_tbl;

5.2、使用where语句查询数据表中的数据。WHERE 子句的字符串比较是不区分大小写的,可以使用 BINARY 关键字来设定 WHERE 子句的字符串比较是区分大小写的。

SELECT column_name,column_name
FROM table_name(table_name1, table_name2...
[WHERE Clause(condition1 [AND [OR]] condition2.....)]
[LIMIT N][ OFFSET M]

 注:

  1、可以使用一个或者多个表,表之间使用逗号(,)分割

  2、WHERE语句设定查询条件

  3、LIMIT 属性来设定返回的记录数

  4、OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0

5.3、使用'like'语句作为where的字句,用百分号 %字符来表示任意字符

mysql> SELECT * from runoob_tbl  WHERE runoob_author LIKE '%COM';

5.4、使用union语句查询多个数据表的数据并删除重复数据;union ALL语句查询多个数据表的数据并返回重复数据

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country; 

 

SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country; 

6、利用order by 字句将select查询数据表数据排序之后再返回

SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]

 注:

  1、ASC表示升序排序,DESC表示降序排列

  2、可以设定多个字段来排序

7、GROUP BY语句根据一个或多个列的结果集进行分组,function可以是COUNT, SUM, AVG等函数,coun(*):计算有多少条记录,sum(column_name) as column_name_count

SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

 统计每个name有多少条记录

mysql> SELECT name, COUNT(*) FROM   employee_tbl GROUP BY name;

 WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…);coalesce 可以取代 NUll 的名称

mysql> SELECT name, SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;

mysql> SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;

 

Guess you like

Origin www.cnblogs.com/yinminbo/p/11790803.html