1.基础知识
1.1如何来查看mysql的帮助手册
?int
Help float;
1.2创建表的规则
CREATE TABLE [IF NOT EXISTS] tbl_name(
字段名 字段类型 [完整性的约束条件]);
1.3如何向表中插入数据
INSERT [INTO] tab_name [(字段名称……)] VALUES (值……)
1.4Mysql数据类型
1.数值型
整数类型
TINYINT:范围0~255;-128~127占用1字节
SMALLINT;范围0~65535-32768~32767;占用2字节
MEDIUMINT;3字节
INT:4字节
BIGINT;8字节
浮点类型
FLOAT(D,,M):占用4字节;两个参数M总共包括几位,D小数点后几位
DOUBLE(M,D):8字节;
定点类型
DECIMAL(D,M):内部以字符串形式存储,占用大小为M+2个字节;
2.字符串类型
CHAR(M)定长 0-255 占M个字节 M代表字符串长度
VARCHAR(M)变长,范围 0-65535,占M+1个字节
TINYTEXT、MEDIUMTEXT、 TEXT、LONGTEXT :只能保存字符数据,内容长度+1个字节
ENUM:最多65535个值,保存值所对应的编号,最多可以存所列举值中的一个
SET:可以选择多个值,最多64个值,保存排列的编号,顺序不区分
3.日期与时间类型
TIME 小时 分钟秒 3个字节
DATE 年月日 3个字节
DATETIME日期时间 8个字节
TIMESTAMP时间戳 4个字节
YEAR 年份 1个字节
2.实验操作
2.1数值型
(1)整型
查看当前服务器下已有的数据库:
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
创建数据库ran并查看是否创建成功:
mysql> CREATE DATABASE ran
-> ;
Query OK, 1 row affected (0.08 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ran |
| test |
+--------------------+
5 rows in set (0.00 sec)
如何使用mysql的帮助手册:\s或者help
查看INT类型的使用范围等,
mysql> ? int
Name: 'INT'
Description:
INT[(M)] [UNSIGNED] [ZEROFILL]
A normal-size integer. The signed range is-2147483648 to 2147483647.
The unsigned range is 0 to 4294967295.?
URL: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html?
查看FLOAT类型的使用范围等,
mysql> help float
Name: 'FLOAT'
Description:
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
A small (single-precision) floating-pointnumber. Permissible values
are -3.402823466E+38 to -1.175494351E-38,0, and 1.175494351E-38 to
3.402823466E+38. These are the theoreticallimits, based on the IEEE
standard. The actual range might beslightly smaller depending on your
hardware or operating system.
M is the total number of digits and D isthe number of digits following
the decimal point. If M and D are omitted,values are stored to the
limits permitted by the hardware. Asingle-precision floating-point
number is accurate to approximately 7decimal places.
UNSIGNED, if specified, disallows negativeva lues.
Using FLOAT might give you some unexpectedproblems because all
calculations in MySQL are done with doubleprecision. See
http://dev.mysql.com/doc/refman/5.5/en/no-matching-rows.html.
URL: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html
创建数据表之前应当使用数据库,登陆mysql后默认没有进入任何数据库,使用数据库ran并查看当前数据库ran所拥有的表,如下所示ran还没有创建任何表:
mysql> USE ran
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)
为测试mysql中的整型创建数据表test1并查看是否创建成功:
mysql> CREATE TABLE IF NOT EXISTS test1(
-> num1 TINYINT,
-> num2 SMALLINT,
-> num3 MEDIUMINT,
-> num4 INT,
-> num5 BIGINT
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> SHOW TABLES;
+---------------+
| Tables_in_ran |
+---------------+
| test1 |
+---------------+
1 row in set (0.00 sec)
向数据表中插入数据,测试mysql的整型:
mysql> INSERT INTO test1VALUES(127,65535,8388607,2147483647,231456789);
ERROR 1264 (22003): Out of range value forcolumn 'num2' at row 1
SMALLINT最大值为32767,超出范围报错,改成32767后插入成功:
mysql> INSERT INTO test1VALUES(127,32767,8388607,2147483647,231456789);
Query OK, 1 row affected (0.03 sec)
查询表中所有记录
SE