数据类型是指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型。(1)TINYINT有符号值: -2^7到2^7-1; 无符号值: 0到2^8-1; 1字节(2)SMALLINT有符号值: -2^15到2^15-1; 无符号值: 0到2^16-1; 2字节(3)MEDIUMINT有符号值: -2^23到2^23-1; 无符号值: 0到2^24-1; 3字节(4)INT有符号值: -2^31到2^31-1; 无符号值: 0到2^32-1; 4字节(5)BIGINT有符号值: -2^63到2^63-1; 无符号值: 0到2^64-1; 8字节1.MySQL数据类型: 整型2.数据优化原则: 选择最合适的数据类型而不是选择最大的。
3、MySQL数据类型: 浮点型float单精度(M,D)M指的是小数点前面和小数点后面的总和数,D是指小数点后面的位数;M>=Ddouble双精度,一般是float存储范围的十倍。两者相比 存储范围越大所占用的空间就越大。当然要用的时候肯定会选择最合适的来用。
3、MySQL数据类型 : 日期时间型(1)、YEAR:1970至2069(2)、TIME:-838:59:59至838:59:59(3)、DATE:1000-1-1至9999-12-31(4)、DATETIME: 1000-1-1 00:00:00至9999-12-31 23:59:59(5)、TIMESTAMP存储范围:1970-1-1 00:00:00到2037-12-31 23:59:59项目开始时,存储时间,一般用整数类型,不用时间,因为一般牵扯到跨时区的问题4、MySQL数据类型: 字符型(1). CHAR(M): M个字节, 0 <= M <= 255, 定长类型(未达到指定长度用空格来补齐)(2). VARCHAR(M): 0 <= M <= 65535, 变长类型(4). TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT最大取值范围分别为1, 2, 3, 4字节(5) ENUM('value1', 'value2', ...): 枚举类型(众多值中选一个), 1或2字节(6). SET('value1', 'value2', ...): 集合类型(集合的成员做排列组合, 最多64个成员), 1, 2, 3, 4或8字节5、数据表
mysql> SHOW DATABASES;数据表是数据库最重要的组成部分之一,是其他对象的基础。use + 数据库名称; 使用数据库create table table_name();5.1、 打开数据库(1) USE 数据库名称;打开数据库(2) SELECT DATABASE();查看当前打开的数据库名称5.2.、创建数据表CREATE TABLE [IF NOT EXISTS] table_name (column_name datatype, ...);(1) ()内column为列名, datatype是数据类型, 合起来为一个字段(2) 两个字段间用逗号隔开, 最后一个字段不加逗号(3) 表内, UNSIGNED表示无符号类型6、展示所有的数据库,打开test数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| t2 |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> USE test;
Database changed
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)7、创建数据表tb
mysql> CREATE TABLE tb1(
-> username VARCHAR(20),
-> age TINYINT UNSIGNED,
-> salary FLOAT(8,2) UNSIGNED
-> );
Query OK, 0 rows affected (0.01 sec)8、查看当前数据库数据表及MySQL所有数据库的数据表
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
+----------------+
1 row in set (0.00 sec)
mysql> SHOW TABLES FROM mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.01 sec)9、查看当前数据库
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)10、查看tb1数据表的结构
mysql> SHOW COLUMNS FROM tb1;
+----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| salary | float(8,2) unsigned | YES | | NULL | |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)11、向数据表中插入数据和查找数据
mysql> INSERT tb1 VALUES('Tom',25,7863.25);
Query OK, 1 row affected (0.02 sec)mysql> INSERT tb1(username,salary) VALUES('John',4500.69);
Query OK, 1 row affected (0.01 sec)mysql> SELECT * FROM tb1;
+----------+------+---------+
| username | age | salary |
+----------+------+---------+
| Tom | 25 | 7863.25 |
| John | NULL | 4500.69 |
+----------+------+---------+
2 rows in set (0.00 sec)12、MySQL的空值与非空并查看
mysql> CREATE TABLE tb2(
-> username VARCHAR(20) NOT NULL,
-> age TINYINT UNSIGNED NULL
-> );
Query OK, 0 rows affected (0.01 sec)mysql> SHOW COLUMNS FROM tb2;
+----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+----------+---------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)mysql> INSERT tb2 VALUES('TOM',NULL);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM tb2;
+----------+------+
| username | age |
+----------+------+
| TOM | NULL |
+----------+------+
1 row in set (0.00 sec)
mysql> INSERT tb2 VALUES(NULL,26);
ERROR 1048 (23000): Column 'username' cannot be null13、MySQL主键约束和自动编号
主键 PRIMARY KEY:用来保证记录的唯一性一张数据表中只能有一个主键,主键自动禁止为空(禁止NOT NULL)主键是使用自动编号(AUTO_INCREMENT)的前提,但设置主键并不需要自动编号作为前提要定义主键,只需要在创建表时,在字段设置的那句代码后面直接加上KEY或者PRIMARY KEY即可保证不重复是指该字段下不重复,但不代表主键外的各字段不出现重复AUTO_INCREMENT必须和PRIMARY KEY 一起使用,每次自增1,可不进行赋值mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || t2 || test |+--------------------+mysql> USE t2;
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)
mysql> CREATE TABLE tb3(
-> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> username VARCHAR(30) NOT NULL
->
-> );Query OK, 0 rows affected (0.02 sec)
mysql> SHOW COLUMNS FROM tb3;
+----------+----------------------+------+-----+---------+----------------
+| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------
+| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment || username | varchar(30) | NO | | NULL | |
+----------+----------------------+------+-----+---------+----------------+2 rows in set (0.00 sec)
mysql> SHOW TABLES;
+--------------+| Tables_in_t2 |
+--------------+| tb3 |
+--------------+1 row in set (0.00 sec)
mysql> INSERT tb3(username) VALUES('Tom');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb3(username) VALUES('John');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb3(username) VALUES('Rose');
Query OK, 1 row affected (0.02 sec)
mysql> INSERT tb3(username) VALUES('Dimitar');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM tb3;
+----+----------+| id | username |+----+----------+| 1 | Tom || 2 | John || 3 | Rose || 4 | Dimitar |+----+----------+4 rows in set (0.00 sec)
不定义AUTO_INCREMENT,可以被赋值但是不允许出现相同的值
mysql> CREATE TABLE tb4(
-> id SMALLINT UNSIGNED PRIMARY KEY,
-> username VARCHAR(20) NOT NULL
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> SHOW COLUMNS FROM tb4;
+----------+----------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+----------------------+------+-----+---------+-------+| id | smallint(5) unsigned | NO | PRI | NULL | || username | varchar(20) | NO | | NULL | |+----------+----------------------+------+-----+---------+-------+2 rows in set (0.02 sec)mysql> INSERT tb4 VALUES(4,'Tom');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb4 VALUES(24,'John');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tb4;
+----+----------+| id | username |+----+----------+| 4 | Tom || 24 | John |+----+----------+2 rows in set (0.00 sec)
mysql> INSERT tb4 VALUES(24,'Rose');
ERROR 1062 (23000): Duplicate entry '24' for key 'PRIMARY'
mysql>
相关文章
- MongoDB新增及查询数据(一)
- 我的起源功能建筑有哪些 功能建筑大全一览
- 量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html
- SpringCloud服务消费者第一次调用出现超时问题的解决方案
- 使用ConcurrentHashMap一定线程安全?
- PHP 获取上一个页面的url
- 莫烦scikit-learn学习自修第一天【scikit-learn安装】
- [POJ2398]Toy Storage(计算几何,二分,判断点在线段的哪一侧)
- (转)一个用D3D绘制2D图形的例子
- Android中使用Canvas绘制简单的图形(一)