1. 程式人生 > >Flask==SQLAlchemy資料型別

Flask==SQLAlchemy資料型別

1 資料型別

型別名 MySQL型別 python型別 描述
SmallInteger smallint int 取值範圍較小,一般為16位
Integer int int 普通整數,一般32位
BigInteger bigint int/long 不限精度的整數
Float float float 浮點數
Numeric decimal decimal.Decimal 定點數
String varchar str 變長字串
Text tinytext str 變長字串,64K, 2 16
1 2^{16}-1
=65535bytes
Text(65536) mediumtext str 變長字串,max16M, 2
24 1 2^{24}-1
=16777215bytes
Text(16777216) longtext str 變長字串,max32M, 2 32 1 2^{32}-1 =4294967295bytes
LargeBinary blob str 二進位制檔案,64K
LargeBinary(65536) mediumblob str 二進位制,max16M
LargeBinary(16777216) longblob str 二進位制,max32M
PickleType blob 任何python物件 自動使用Pickle序列化,只有blog
Unicode varchar unicode 變長字串
UnicodeText text unicode 變長字串,64K
Boolean tinyint bool 布林值
Date date datetime.date 日期
Time time date.time 時間
DateTime datetime datetime.datetime 日期和時間
Interval datetime datetime.timedelta 時間間隔
Enum enum str 一組字串

2 資料庫型別設計

  • 建立MySQL表
class Data(db.Model):
	__tablename__ = "datas"
	id = db.Column(db.Integer, primary_key=True)
	smallInteger = db.Column(db.SmallInteger)
	bigInteger = db.Column(db.BigInteger)
	floatData = db.Column(db.Float(10))
	numericData = db.Column(db.Numeric(10))
	stringData = db.Column(db.String(250))
	textData = db.Column(db.Text(200))
	mediumText = db.Column(db.Text(65536))
	longText = db.Column(db.Text(16777216))
	largeBinary = db.Column(db.LargeBinary(300))
	mediumBlob = db.Column(db.LargeBinary(65536))
	longBlob = db.Column(db.LargeBinary(16777216))
	pickle = db.Column(db.PickleType)
	mediumPickle = db.Column(db.PickleType(65536))
	longPickle = db.Column(db.PickleType(16777216))
	unicodeData = db.Column(db.Unicode(10))
	unicodeText = db.Column(db.UnicodeText)
	booleanData = db.Column(db.Boolean(0))
	dateData = db.Column(db.Date)
	timeData = db.Column(db.Time)
	dateTime = db.Column(db.DateTime)
	interval = db.Column(db.Interval)
	enumData = db.Column(db.Enum('father', 'mother'))
	def __repr__(self):
		return "Data {}".format(self.id)
	
  • MySQL表結構
+--------------+-------------------------+------+-----+---------+----------------+
| Field        | Type                    | Null | Key | Default | Extra          |
+--------------+-------------------------+------+-----+---------+----------------+
| id           | int(11)                 | NO   | PRI | NULL    | auto_increment |
| smallInteger | smallint(6)             | YES  |     | NULL    |                |
| bigInteger   | bigint(20)              | YES  |     | NULL    |                |
| floatData     |  float                   | YES  |     | NULL    |                |
| numericData  | decimal(10,0)           | YES  |     | NULL    |                |
| stringData   | varchar(250)            | YES  |     | NULL    |                |
| textData     | tinytext                | YES  |     | NULL    |                |
| mediumText   | mediumtext              | YES  |     | NULL    |                |
| longText     | longtext                | YES  |     | NULL    |                |
| largeBinary  | blob                    | YES  |     | NULL    |                |
| mediumBlob   | mediumblob              | YES  |     | NULL    |                |
| longBlob     | longblob                | YES  |     | NULL    |                |
| pickle       | blob                    | YES  |     | NULL    |                |
| mediumPickle | blob                    | YES  |     | NULL    |                |
| longPickle   | blob                    | YES  |     | NULL    |                |
| unicodeData  | varchar(10)             | YES  |     | NULL    |                |
| unicodeText  | text                    | YES  |     | NULL    |                |
| booleanData  | tinyint(1)              | YES  |     | NULL    |                |
| dateData     | date                    | YES  |     | NULL    |                |
| timeData     | time                    | YES  |     | NULL    |                |
| dateTime     | datetime                | YES  |     | NULL    |                |
| interval     | datetime                | YES  |     | NULL    |                |
| enumData     | enum('father','mother') | YES  |     | NULL    |                |
+--------------+-------------------------+------+-----+---------+----------------+

3 Flask-MySQL鍵屬性

屬性 描述 生效值
primary_key 主鍵 True
unique 鍵值唯一性 True
index 索引 True
nullable 空值 True
default 預設值 null

[參考文獻]

[1]https://www.cnblogs.com/Orangeorchard/p/8097547.html
[2]https://blog.csdn.net/skdkjzz/article/details/20469721