MySQL - TINYINT



MySQL TINYINT 数据类型

MySQL 的TINYINT数据类型用于存储范围很小的整数值。它只占用 1 字节(8 位)的存储空间,对于带符号的 TINYINT,可以存储 -128 到 127 的值;对于无符号的 TINYINT,可以存储 0 到 255 的值。

在 MySQL 中定义 TINYINT 列时,默认情况下它被认为是带符号的。这意味着它可以在特定范围内保存正数和负数。此外,可以使用“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

同样,如果我们将 -128 插入 TINYINT ZEROFILL 列 (“col3”),则会生成错误:

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/Pythonexecute()函数执行“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://127.0.0.1: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)
广告