day25 mysql数据库入门 (4)

text

text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。 一般情况下,长文本会用text类型。例如:文章、新闻等。 create table L4( id int not null primary key auto_increment, title varchar(128), content text )default charset=utf8;

mediumtext

A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.

longtext

A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1)

datetime

YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)

timestamp

YYYY-MM-DD HH:MM:SS(1970-01-01 00:00:00/2037年) 对于TIMESTAMP,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储,查询时,将其又转化为客户端当前时区进行返回。 对于DATETIME,不做任何改变,原样输入和输出。 mysql> create table L5( -> id int not null primary key auto_increment, -> dt datetime, -> tt timestamp -> )default charset=utf8; Query OK, 0 rows affected (0.03 sec) mysql> insert into L5(dt,tt) values("2025-11-11 11:11:44", "2025-11-11 11:11:44"); mysql> select * from L5; +----+---------------------+---------------------+ | id | dt | tt | +----+---------------------+---------------------+ | 1 | 2025-11-11 11:11:44 | 2025-11-11 11:11:44 | +----+---------------------+---------------------+ 1 row in set (0.00 sec) mysql> show variables like \'%time_zone%\'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | SYSTEM | +------------------+--------+ 2 rows in set (0.00 sec) -- “CST”指的是MySQL所在主机的系统时间,是中国标准时间的缩写,China Standard Time UT+8:00 mysql> set time_zone=\'+0:00\'; Query OK, 0 rows affected (0.00 sec) mysql> show variables like \'%time_zone%\'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | +00:00 | +------------------+--------+ 2 rows in set (0.01 sec) mysql> select * from L5; +----+---------------------+---------------------+ | id | dt | tt | +----+---------------------+---------------------+ | 1 | 2025-11-11 11:11:44 | 2025-11-11 03:11:44 | +----+---------------------+---------------------+ 1 row in set (0.00 sec)

date

YYYY-MM-DD(1000-01-01/9999-12-31)

time

HH:MM:SS(\'-838:59:59\'/\'838:59:59\')

MySQL还有很多其他的数据类型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob 等,详细见官方文档:https://dev.mysql.com/doc/refman/5.7/en/data-types.html

上述就是关于数据表的一些基本操作。

3.2 MySQL代码操作

基于Python去连接MySQL之后,想要进行数据表的管理的话,发送的指令其实都是相同的,例如:

import pymysql # 连接MySQL conn = pymysql.connect(host=\'127.0.0.1\', port=3306, user=\'root\', passwd=\'root123\', charset="utf8") cursor = conn.cursor() # 1. 创建数据库 """ cursor.execute("create database db4 default charset utf8 collate utf8_general_ci") conn.commit() """ # 2. 进入数据库、查看数据表 """ cursor.execute("use db4") cursor.execute("show tables") result = cursor.fetchall() print(result) """ # 3. 进入数据库创建表 cursor.execute("use db4") sql = """ create table L4( id int not null primary key auto_increment, title varchar(128), content text, ctime datetime )default charset=utf8; """ cursor.execute(sql) conn.commit() # 4. 查看数据库中的表 """ cursor.execute("show tables") result = cursor.fetchall() print(result) """ # 5. 其他 drop table... 略过 # 关闭连接 cursor.close() conn.close() 4.数据行

当数据库和数据表创建完成之后,就需要对数据表中的内容进行:增、删、改、查了。

day25 mysql数据库入门

4.1 内置客户端操作

数据行操作的相关SQL语句(指令)如下:

数据

insert into 表名 (列名,列名,列名) values(对应列的值,对应列的值,对应列的值); insert into tb1(name,password) values(\'武沛齐\',\'123123\'); insert into tb1(name,password) values(\'武沛齐\',\'123123\'),(\'alex\',\'123\'); insert into tb1 values(\'武沛齐\',\'123123\'),(\'alex\',\'123\'); -- 如果表中只有2列

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzgswx.html