一、数据库常用命令
1 创建数据库 1.1 创建mydb1数据库,使用默认字符集 create database mydb1; 1.2 创建数据库mydb2,字符集使用utf8 create database mydb2 character set utf8 1.3 创建一个使用utf-8数据库mydb3,并带有校对规则 create database mydb3 character set utf8 collate utf8_general_ci
2 删除数据库drop database if exists mydb2
3 查看所有的数据库show databases;
4 指定使用某个数据库use mydb2;
5 查看当前处于哪个数据库show tables;
二、MySQL数据类型
1 整数类型 1.1 bit(M) M为允许存储M位值,M默认为1,最大值为64 例子: create table test1( id bit(1) ); 1.2 tinyint tinyint默认是有符号,有符号范围是-128 至 127 create table test2( id tinyint ); tinyint 无符号,无符号范围是0至255 create table test2( id tinyint unsigned ); 1.3 smallint 是两个字节表示的 有符号范围:-215至215-1 无符号范围:0 至 216-1 1.4 decimal 和 numeric 是变长
2 浮点(小数)类型 float 占4个字节 float[(m,d)] m:表示整数有效位数 d:表示小数点后面位数 2.1 float 不指名小数保留位数 create table test4( id float ); insert into test4 values (77.7777); 备注:float没有指定整数位和小数位,则插入什么数据则显示什么数据 2.2 float指名小数点保留位数 create table test5( id float(5,1) ); insert into test4 values(77.7777); 存入的数据做四舍五入的办法处理 2.3 double 占8个字节,处理方式与float类似 2.4 numeric[(m,d)] m:表示整数有效位数 d:表示小数点后面位数 create table test6( id numeric ); 没有指明小数点后面保留位数,则该字段则存放整数 create table test7( id numeric(7,2) # 表示有效位为7位,小数点有两位 ); 指明小数点后面保留位数,则该字段则存放小数
3、字符串类型
char(m)
0<=m<=255
文本
varchar(m)
0<=m<=65532
文本
binary(m)
0<=m<=255
二进制
blob
216
二进制
text
216
文本
mediumblob
224
二进制
mediumtext
224
文本
longblob
232
二进制
longtext
232
文本
ENUM
65535
SET
63
4、日期类型
mysql> select now() from dual; +---------------------+ | now() | +---------------------+ | 2014-11-10 11:52:44 | +---------------------+ 备注:now()是获取数据库当前时间,dual是一个虚拟表,用于测试。
4.1 date —— 年月日 create table test9( birthday date );
mysql> create table test9( -> birthday date -> );Query OK, 0 rows affected (0.17 sec)
mysql> insert into test9 values(now());Query OK, 1 row affected, 1 warning (0.12 sec)
mysql> select * from test9;+------------+| birthday |+------------+| 2014-11-11 |+------------+1 row in set (0.00 sec)
4.2 datetime —— 年月日时分秒 create table test10( birthday datetime ); 4.3 timestamp —— 年月日时分秒 它和datetime最大的区别是,当你update某条记录的时候,该列的值就自动更新。 create table test12( name varchar(64), comein datetime, birthday timestamp );
大小: 2.6 KB 大小: 2.2 KB 大小: 2.3 KB 大小: 5.5 KB 大小: 6.2 KB 大小: 10.2 KB 大小: 21.7 KB 查看图片附件