MySQL - TINYINT
- MySQL TINYINT 数据类型
- 使用客户端程序的 TINYINT 数据类型
MySQL TINYINT 数据类型
MySQL TINYINT 数据类型用于存储非常小范围内的整数值。它仅占用 1 byte(8 bits)的存储空间,对于 signed TINYINT 可以存储 -128 到 127 的值,对于 unsigned TINYINT 可以存储 0 到 255 的值。
在 MySQL 中定义 TINYINT 列时,默认视为 SIGNED。这意味着它可以在特定范围内存储正数和负数。此外,您可以使用 "TINYINT" 或 "INT1" 来定义此类列,因为它们的工作方式相同。
语法
以下是 MySQL TINYINT 数据类型的语法 −
TINYINT(M) [SIGNED | UNSIGNED | ZEROFILL]
示例
首先,使用以下查询创建一个名为 tinyint_table 的表 −
CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL );
以下是得到的结果 −
Query OK, 0 rows affected, 1 warning (0.03 sec)
现在,让我们尝试向这些列插入一些值 (128, 128, 128),如下所示 −
INSERT INTO tinyint_table VALUES (128, 128, 128);
对于 col1 中的值会产生错误,因为我们插入的值超出了范围 −
ERROR 1264 (22003): Out of range value for column 'col1' at row 1
接下来,如果我们尝试向 TINYINT UNSIGNED 列 ("col2") 插入负值,将导致错误,因为 UNSIGNED 值不能为负 −
INSERT INTO tinyint_table VALUES (127, -120, 128);
显示的错误消息如下 −
ERROR 1264 (22003): Out of range value for column 'col2' at row 1
类似地,如果我们向 TINYINT ZEROFILL 列 ("col3") 插入 -128,将产生错误 −
INSERT INTO tinyint_table VALUES (127, 128, -128);
输出如下所示 −
ERROR 1264 (22003): Out of range value for column 'col3' at row 1
但是,如果我们插入有效范围内的值,插入将成功,如下所示 −
INSERT INTO tinyint_table VALUES (127, 128, 128);
以上代码的输出如下 −
Query OK, 1 row affected (0.01 sec)
最后,我们可以使用以下 SELECT 查询检索表中的所有记录 −
SELECT * FROM tinyint_table;
此查询将显示以下结果 −
| col1 | col2 | col3 |
|---|---|---|
| 127 | 128 | 128 |
使用客户端程序的 TINYINT 数据类型
我们也可以使用客户端程序创建 TINYINT 数据类型的列。
语法
通过 PHP 程序创建 TINYINT 数据类型的列,我们需要使用 mysqli 函数 query() 执行 "CREATE TABLE" 语句,如下所示 −
$sql = 'CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )'; $mysqli->query($sql);
通过 JavaScript 程序创建 TINYINT 数据类型的列,我们需要使用 mysql2 库的 query() 函数执行 "CREATE TABLE" 语句,如下所示 −
sql = "CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )"; con.query(sql);
通过 Java 程序创建 TINYINT 数据类型的列,我们需要使用 JDBC 函数 execute() 执行 "CREATE TABLE" 语句,如下所示 −
String sql = "CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )"; statement.execute(sql);
通过 Python 程序创建 TINYINT 数据类型的列,我们需要使用 MySQL Connector/Python 的 execute() 函数执行 "CREATE TABLE" 语句,如下所示 −
sql = 'CREATE TABLE tinyint_table (col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL)' cursorObj.execute(sql)
示例
以下是相应的程序 −
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
//printf('Connected successfully.
');
$sql = 'CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )';
$result = $mysqli->query($sql);
if ($result) {
printf("Table created successfully...!\n");
}
// insert data into created table
$q = " INSERT INTO tinyint_table (col1, col2, col3) VALUES (100, 105, 110)";
if ($res = $mysqli->query($q)) {
printf("Data inserted successfully...!\n");
}
//now display the table records
$s = "SELECT * FROM tinyint_table";
if ($r = $mysqli->query($s)) {
printf("Table Records: \n");
while ($row = $r->fetch_assoc()) {
printf(" Col_1: %s, Col_2: %s, Col_3: %s", $row["col1"], $row["col2"], $row["col3"]);
printf("\n");
}
} else {
printf('Failed');
}
$mysqli->close();
输出
得到的输出如下所示 −
Table created successfully...! Data inserted successfully...! Table Records: Col_1: 100, Col_2: 105, Col_3: 110
var mysql = require("mysql2");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
}); //Connecting to MySQL
con.connect(function (err) {
if (err) throw err;
// console.log("Connected successfully...!");
// console.log("--------------------------");
sql = "USE TUTORIALS";
con.query(sql);
//create a tinyint_table table, that accepts one column of tinyint type.
sql = "CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )";
con.query(sql);
//insert data into created table
sql =
"INSERT INTO tinyint_table (col1, col2, col3) VALUES (100, 105, 110)";
con.query(sql);
//select datatypes of salary
sql = `SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tinyint_table' AND COLUMN_NAME = 'col2'`;
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
输出
产生的输出如下所示 −
[ { DATA_TYPE: 'tinyint' } ]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TinyInt {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/TUTORIALS";
String username = "root";
String password = "password";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
System.out.println("Connected successfully...!");
//TinyInt data types...!;
String sql = "CREATE TABLE tinyint_table ( col1 TINYINT, col2 TINYINT UNSIGNED, col3 TINYINT ZEROFILL )";
statement.execute(sql);
System.out.println("column of a TINYINT type created successfully...!");
ResultSet resultSet = statement.executeQuery("DESCRIBE tinyint_table");
while (resultSet.next()){
System.out.println(resultSet.getString(1)+" "+resultSet.getString(2));
}
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
输出
得到的输出如下图所示 −
Connected successfully...! column of a TINYINT type created successfully...! col1 tinyint col2 tinyint unsigned col3 tinyint(3) unsigned zerofill
import mysql.connector
# Establishing the connection
connection = mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='tut'
)
# Creating a cursor object
cursorObj = connection.cursor()
# Create table with Tinyint column
sql = '''
CREATE TABLE tinyint_table (
col1 TINYINT,
col2 TINYINT UNSIGNED,
col3 TINYINT ZEROFILL
)'''
cursorObj.execute(sql)
print("The table is created successfully!")
# Insert data into the created table
insert_query = "INSERT INTO tinyint_table (col1, col2, col3) VALUES (127, 128, 128);"
cursorObj.execute(insert_query)
# Commit the changes after the insert operation
connection.commit()
print("Rows inserted successfully.")
# Now display the table records
select_query = "SELECT * FROM tinyint_table"
cursorObj.execute(select_query)
result = cursorObj.fetchall()
print("Table Data:")
for row in result:
print(row)
cursorObj.close()
connection.close()
输出
上述代码的输出如下 −
The table is created successfully! Rows inserted successfully. Table Data: (127, 128, 128)