mysql - database operations and data attributes

Database operations

Start mysql, macby brewinstalling mysqlstart later. windowYou need to manually configure

1
2
3
4
5
6
7
8
9
10
11
12
13
14

mysql.server start


net start mysql

// login user name -u root -p superuser password
mysql -uroot -p

// Exit
mysql> q

// switch to learn the database
mysql> use learn

MySQL statement specification

  • Keywords and function names are all uppercase
  • Database names, table names, field names all lower case
  • SQL statements must end with a semicolon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// create database-8 default encoding UTF
the CREATE DATABASE IF the NOT EXISTS T1;

// Create gbk coding database
CREATE DATABASE IF NOT EXISTS t2 CHARACTER SET gbk;

// Create a database t1 show command
SHOW CREATE DATABASE t1;

// Check database
SHOW DATABASES;

// delete the database
DROP DATABASE IF EXISTS t1;

type of data

Integer

MySQL Data Types meaning (Signed)
tinyint(m) 1 byte Range (-128 to 127)
smallint(m) 2 bytes Range (-32768 to 32767)
mediumint(m) 3 byte Range (~ -8388608 8388607)
int(m) 4 bytes Range (-2147483648 to 2147483647)
bigint(m) 8 bytes Range (18 + -9.22 * 10 th)

For example, we store the age range is 0-100, then we can use TINYINTto store

Float (float and double)

MySQL Data Types meaning
float(m,d) Total single precision floating point precision of eight bits (4 bytes) m number, d decimal places
double(m,d) The total 16-bit double precision floating point precision (8 bytes) m number, d decimal places

Field is defined as a set float (6,3), if a number is inserted 123.45678, actually stored in the database is 123.457, but also the total number of the actual subject, i.e., six. The integer portion of the maximum is three, if the number of inserted 12.123456, 12.1234 is stored, if inserted 12.12, storage is 12.1200.

String (char, varchar, _text)

MySQL Data Types meaning
char(n) Fixed length, up to 255 characters
varchar(n) Fixed length, up to 65,535 characters.
tinytext Variable length, up to 255 characters
text Variable length, up to 65,535 characters.
mediumtext Variable length, a maximum power of 24 characters -1 2
longtext Variable length, up to 32 characters power of 2 -1

charAnd varchar:

1. char(n)If the stored number of characters is less than n, it places the space up, and then remove the spaces of the query. Char type is stored so the end of the string can not have spaces, varcharis not limited thereto.

2.char(n) 固定长度,char(4)不管是存入几个字符,都将占用4个字节,varchar是存入的实际字符数+1个字节(n<=255)或2个字节(n>255),

所以varchar(4),存入3个字符将占用4个字节。

3.char类型的字符串检索速度要比varchar类型的快。
varchartext

1.varchar可指定n,text不能指定,内部存储varchar是存入的实际字符数+1个字节(n<=255)或2个字节(n>255),text是实际字符数+2个字

节。

2.text类型不能有默认值。

3.varchar可直接创建索引,text创建索引要指定前多少个字符。varchar查询速度快于text,在都创建索引的情况下,text的索引似乎不起作用。

5.二进制数据(_Blob)

1._BLOB和_text存储方式不同,_TEXT以文本方式存储,英文存储区分大小写,而_Blob是以二进制方式存储,不分大小写。

2._BLOB存储的数据只能整体读出。

3._TEXT可以指定字符集,_BLO不用指定字符集。

日期时间类型

MySQL数据类型 含义
date 日期 ‘2008-12-2’
time 时间 ‘12:25:36’
datetime 日期时间 ‘2008-12-2 22:06:44’
timestamp 自动存储记录修改时间

数据类型的属性

MySQL关键字 含义
NULL 数据列可包含NULL值
NOT NULL 数据列不允许包含NULL值
DEFAULT 默认值
PRIMARY KEY 主键
AUTO_INCREMENT 自动递增,适用于整数类型
UNSIGNED 无符号
CHARACTER SET name Specify a character set

Original: Big Box  mysql - database operations and data attributes


Guess you like

Origin www.cnblogs.com/petewell/p/11422222.html