如果您对各数据库(MySQL、PostgreSQL、Oracle、MsSQL)有关自增字段的设置感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于各数据库(MySQL、Post
如果您对各数据库(MySQL、PostgreSQL、Oracle、MsSQL)有关自增字段的设置感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于各数据库(MySQL、PostgreSQL、Oracle、MsSQL)有关自增字段的设置的详细内容,我们还将为您解答数据库自增字段怎么写的相关问题,并且为您提供关于(转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)、C# 多数据库组件包,支持 MSSQL+Oracle+MySQL + 用户操作手册 | C/S 开发框架、C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)、centos下docker自动备份 mysql、mssql的有价值信息。
本文目录一览:- 各数据库(MySQL、PostgreSQL、Oracle、MsSQL)有关自增字段的设置(数据库自增字段怎么写)
- (转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
- C# 多数据库组件包,支持 MSSQL+Oracle+MySQL + 用户操作手册 | C/S 开发框架
- C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
- centos下docker自动备份 mysql、mssql
各数据库(MySQL、PostgreSQL、Oracle、MsSQL)有关自增字段的设置(数据库自增字段怎么写)
在数据库设计过程中,我们通常会把数据库表中的ID字段设置成自增。下面以常用的数据字典表为例,说明如何在各数据库下设置自增字段。
MySQL
MySQL数据库只需要在目标字段上添加AUTO_INCREMENT,并且为表设置AUTO_INCREMENT=x。
x:自增开始的数字。
参考示例:
CREATE TABLE `dictionary` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ''ID'',
`parent_id` int(10) unsigned NOT NULL COMMENT ''父ID'',
`type` varchar(50) NOT NULL COMMENT ''元数据类型'',
`item_name` varchar(100) NOT NULL COMMENT ''元数据项显示名'',
`item_value` varchar(100) DEFAULT NULL COMMENT ''元数据项存储值'',
`comment` varchar(200) DEFAULT NULL COMMENT ''备注'',
`deleted` tinyint(1) NOT NULL DEFAULT ''0'' COMMENT ''删除标记'',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT=''数据字典'';
PostgreSQL
PostgreSQL数据库有多种方法可实现自增字段的设置,常用的有:
- SERIAL,最简单
- IDENTITY,是PostgreSQL 10的新增特性
- 创建SEQUENCE,更灵活
参考示例:SERIAL
create table dictionary (
id SERIAL not null,
parent_id INT4 not null,
type VARCHAR(50) not null,
item_name VARCHAR(100) not null,
item_value VARCHAR(100) null,
comment VARCHAR(200) null,
deleted INT2 not null default 0,
create_time DATE not null default CURRENT_TIMESTAMP,
constraint PK_dictionary primary key (id)
);
Oracle
Oracle数据库常用的设置自增字段的两种方法:
- 创建SEQUENCE
- IDENTITY,要求Oracle数据库版本是12c或以上
12c版本示例:
create table "dictionary" (
"id" INTEGER generated as identity ( start with 1 nocycle noorder) not null,
"parent_id" INTEGER not null,
"type" VARCHAR2(50) not null,
"item_name" VARCHAR2(100) not null,
"item_value" VARCHAR2(100),
"comment" VARCHAR2(200),
"deleted" SMALLINT default 0 not null,
"create_time" TIMESTAMP default CURRENT_TIMESTAMP not null,
constraint "PK_dictionary" primary key ("id")
);
11g版本示例:
-- 建表
create table "dictionary" (
"id" INTEGER not null,
"parent_id" INTEGER not null,
"type" VARCHAR2(50) not null,
"item_name" VARCHAR2(100) not null,
"item_value" VARCHAR2(100),
"comment" VARCHAR2(200),
"deleted" SMALLINT default 0 not null,
"create_time" TIMESTAMP default CURRENT_TIMESTAMP not null,
constraint "PK_dictionary" primary key ("id")
);
-- 创建序列
CREATE SEQUENCE DICTIONARY_ID_SEQ
INCREMENT BY 1
START WITH 1;
-- 创建触发器,非必须
CREATE OR REPLACE TRIGGER DICTIONARY_ID_SEQ_TRG
BEFORE INSERT ON "dictionary"
FOR EACH ROW
WHEN (NEW."id" IS NULL)
BEGIN
SELECT DICTIONARY_ID_SEQ.NEXTVAL
INTO :NEW."id"
FROM DUAL;
END;
MsSQL
MsSQL即SQL Server数据库,使用IDENTITY即可。
参考示例:
create table dictionary (
id int identity,
parent_id int not null,
type varchar(50) not null,
item_name varchar(100) not null,
item_value varchar(100) null,
comment varchar(200) null,
deleted smallint not null default 0,
create_time datetime not null default CURRENT_TIMESTAMP,
constraint PK_dictionary primary key (id)
);
diboot 简单高效的轻代码开发框架 (求star)
(转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
一、C# vs SQLite: C#SQLite 字段名类型库类型GetFieldType(#)转换备注 F_BOOLboolBIT NOT NULLBoolean F_BOOL_NULLbool?BITBoolean F_SBYTEsbyteINT8 NOT NULLSBytesbyte_ F_SBYTE_NULLsbyte?INT8SBytesbyte_ F_BYTEbyteUINT8 NOT NULLByte F_BYTE_NULLbyte
一、c# vs sqlite:
C# SQLite
字段名 类型 库类型 GetFieldType(#) 转换 备注
F_BOOL bool BIT NOT NULL Boolean
F_BOOL_NULL bool? BIT Boolean
F_SBYTE sbyte INT8 NOT NULL SByte sbyte_
F_SBYTE_NULL sbyte? INT8 SByte sbyte_
F_BYTE byte UINT8 NOT NULL Byte
F_BYTE_NULL byte? UINT8 Byte
F_SHORT short INT16 NOT NULL Int16
F_SHORT_NULL short? INT16 Int16
F_USHORT ushort UINT16 NOT NULL UInt16 ushort_
F_USHORT_NULL ushort? UINT16 UInt16 ushort_
F_INT int INT32 NOT NULL Int32
F_INT_NULL int? INT32 Int32
F_UINT uint UINT32 NOT NULL UInt32 uint_
F_UINT_NULL uint? UINT32 UInt32 uint_
F_LONG long INT64 NOT NULL Int64
F_LONG_NULL long? INT64 Int64
F_ULONG ulong UINT64 NOT NULL UInt64 ulong_
F_ULONG_NULL ulong? UINT64 UInt64 ulong_
F_FLOAT float FLOAT NOT NULL Double 不转兼容
F_FLOAT_NULL float? FLOAT Double 不转兼容
F_DOUBLE double DOUBLE NOT NULL Double
F_DOUBLE_NULL double? DOUBLE Double
F_DECIMAL
decimal
DECIMAL NOT NULL
Decimal
存取不一:
F_DECIMAL_NULL
decimal?
DECIMAL
Decimal
存取不一:
F_CHAR char TEXT NOT NULL String
F_CHAR_NULL char? TEXT String
F_STRING string TEXT NOT NULL String
F_STRING_NULL string TEXT String
F_DATETIME DateTime TEXT NOT NULL String
F_DATETIME_NULL DateTime? TEXT String
二、 C# vs MySQL:
C# MySQL
字段名 类型 库类型 GetFieldType(#) 转换 备注
F_BOOL bool BOOL NOT NULL Boolean
F_BOOL_NULL bool? BOOL NULL Boolean
F_SBYTE sbyte TINYINT NOT NULL SByte sbyte_
F_SBYTE_NULL sbyte? TINYINT NULL SByte sbyte_
F_BYTE byte TINYINT UNSIGNED NOT NULL Byte
F_BYTE_NULL byte? TINYINT UNSIGNED NULL Byte
F_SHORT short SMALLINT NOT NULL Int16
F_SHORT_NULL short? SMALLINT NULL Int16
F_USHORT ushort SMALLINT UNSIGNED NOT NULL UInt16 ushort_
F_USHORT_NULL ushort? SMALLINT UNSIGNED NULL UInt16 ushort_
F_INT int INT NOT NULL Int32
F_INT_NULL int? INT NULL Int32
F_UINT uint INT UNSIGNED NOT NULL UInt32 uint_
F_UINT_NULL uint? INT UNSIGNED NULL UInt32 uint_
F_LONG long BIGINT NOT NULL Int64
F_LONG_NULL long? BIGINT NULL Int64
F_ULONG ulong BIGINT UNSIGNED NOT NULL UInt64 ulong_
F_ULONG_NULL ulong? BIGINT UNSIGNED NULL UInt64 ulong_
F_FLOAT float FLOAT NOT NULL Single 极值溢出
F_FLOAT_NULL float? FLOAT NULL Single 极值溢出
F_DOUBLE double DOUBLE NOT NULL Double
F_DOUBLE_NULL double? DOUBLE NULL Double
F_DECIMAL decimal DECIMAL NOT NULL Decimal 极值溢出
F_DECIMAL_NULL decimal? DECIMAL NULL Decimal 极值溢出
F_CHAR char CHARACTER NOT NULL String 中文报错
F_CHAR_NULL char? CHARACTER NULL String 中文报错
F_STRING string VARCHAR(50) NOT NULL String
F_STRING_NULL string VARCHAR(50) NULL String
F_DATETIME DateTime DATETIME NOT NULL DateTime
F_DATETIME_NULL DateTime? DATETIME NULL DateTime
三、 C# vs MSSQL:
C# MSSQL
字段名 类型 库类型 GetFieldType(#) 转换 备注
F_BOOL bool bit NOT NULL Boolean
F_BOOL_NULL bool? bit NULL Boolean
F_SBYTE sbyte smallint NOT NULL Int16 sbyte_short
F_SBYTE_NULL sbyte? smallint NULL Int16 sbyte_short
F_BYTE byte tinyint NOT NUL; Byte
F_BYTE_NULL byte? tinyint NULL Byte
F_SHORT short smallint NOT NULL Int16
F_SHORT_NULL short? smallint NULL Int16
F_USHORT ushort int NOT NULL Int32 ushort_int
F_USHORT_NULL ushort? int NULL Int32 ushort_int
F_INT int int NOT NULL Int32
F_INT_NULL int? int NULL Int32
F_UINT uint bigint NOT NULL Int64 uint_long
F_UINT_NULL uint? bigint NULL Int64 uint_long
F_LONG long bigint NOT NULL Int64
F_LONG_NULL long? bigint NULL Int64
F_ULONG ulong real NOT NULL Single ulong_float
F_ULONG_NULL ulong? real NULL Single ulong_float
F_FLOAT float real NOT NULL Single
F_FLOAT_NULL float? real NULL Single
F_DOUBLE double float NOT NULL Double
F_DOUBLE_NULL double? float NULL Double
F_DECIMAL decimal decimal NOT NULL Decimal 极值溢出
F_DECIMAL_NULL decimal? decimal NULL Decimal 极值溢出
F_CHAR char char(1) NOT NULL String
F_CHAR_NULL char? char(1) NULL String
F_STRING string varchar(50) NOT NULL F_STRING:
F_STRING_NULL string varchar(50) NULL String
F_DATETIME DateTime datetime NOT NULL DateTime
F_DATETIME_NULL DateTime? datetime NULL DateTime
四、C# vs Oracle:
C# Oracle
字段名 类型 库类型 GetFieldType(#) 转换 备注
F_BOOL bool CHAR(1) NOT NULL String bool_string
F_BOOL_NULL bool? CHAR(1) String bool_string
F_SBYTE sbyte NUMBER(3) NOT NULL Int16 sbyte_short
F_SBYTE_NULL sbyte? NUMBER(3) Int16 sbyte_short
F_BYTE byte NUMBER(3) NOT NULL Int16 byte_short
F_BYTE_NULL byte? NUMBER(3) Int16 byte_short
F_SHORT short NUMBER(5) NOT NULL Int32 short_int
F_SHORT_NULL short? NUMBER(5) Int32 short_int
F_USHORT ushort NUMBER(5) NOT NULL Int32 ushort_int
F_USHORT_NULL ushort? NUMBER(5) Int32 ushort_int
F_INT int NUMBER(10) NOT NULL Int64 int_long
F_INT_NULL int? NUMBER(10) Int64 int_long
F_UINT uint NUMBER(10) NOT NULL Int64 uint_long
F_UINT_NULL uint? NUMBER(10) Int64 uint_long
F_LONG long NUMBER(19) NOT NULL Decimal long_decimal
F_LONG_NULL long? NUMBER(19) Decimal long_decimal
F_ULONG ulong NUMBER(19) NOT NULL Decimal ulong_decimal
F_ULONG_NULL ulong? NUMBER(19) Decimal ulong_decimal
F_FLOAT float BINARY_FLOAT NOT NULL Single
F_FLOAT_NULL float? BINARY_FLOAT Single
F_DOUBLE double BINARY_DOUBLE NOT NULL Double 极值溢出
F_DOUBLE_NULL double? BINARY_DOUBLE Double 极值溢出
F_DECIMAL decimal DECIMAL(33,3) NOT NULL Decimal
F_DECIMAL_NULL decimal? DECIMAL(33,3) Decimal
F_CHAR char CHAR(1) NOT NULL String
F_CHAR_NULL char? CHAR(1) String
F_STRING string VARCHAR(50) NOT NULL String 不允许空字符
F_STRING_NULL string VARCHAR(50) String 不允许空字符
F_DATETIME DateTime TIMESTAMP NOT NULL DateTime 大值读取报错
F_DATETIME_NULL DateTime? TIMESTAMP DateTime 大值读取报
C# 多数据库组件包,支持 MSSQL+Oracle+MySQL + 用户操作手册 | C/S 开发框架
C# 多数据库组件包支持 MSSQL+Oracle+MySQL + 用户操作手册 | C/S 框架网
我们在开发软件过程中,特别是基于数据管理应用相关的系统,一般采用一种数据库,如 Microsoft SQL Server,超大型系统有的使用 Oracle,部分 Web 系统采用开源的 MySQL 等,由于各种业务场景以及用户需求,促使我们的数据库系统部署在不同类型的数据库服务器上。若开发的系统能支持多种数据库的快速切换,可以为我们减少很多烦恼,同时提高系统的适应性、兼容性以及可扩展性。
/// <summary>
/// IDatabase 数据库接口
/// </summary>
public interface IDatabase : IAdoFactory
{
int DefaultPort { get; }
int CommandTimeout { get; }
int ConnectionTimeout { get; }
Type DateTimeType { get; }
DateTime GetServerTime();
String ConnectionString { get; set; }
DbTransaction TransBegin();
void TransCommit(DbTransaction trans, bool closeConnection = false);
void TransRollback(DbTransaction trans, bool closeConnection = false);
DataSet GetDataSet(string SQL);
DataSet GetDataSet(DbCommand cmd);
DataSet GetDataSet(string text, CommandType type, IDataParameter[] paras);
DataTable GetTable(string SQL, string tableName = "");
DataTable GetTable(DbCommand cmd, string tableName = "");
DataRow GetDataRow(string SQL);
int ExecuteSQL(string SQL);
int ExecuteCommand(DbCommand cmd);
object ExecuteScalar(string SQL);
object ExecuteScalar(DbCommand cmd);
DbDataReader ExecuteReader(string SQL);
DbDataReader ExecuteReader(DbCommand cmd);
List<T> ExecuteReader<T>(string SQL, Func<DbDataReader, T> action) where T : new();
List<T> ExecuteReader<T>(DbCommand cmd, Func<DbDataReader, T> action) where T : new();
T ExecuteReader<T>(DbCommand cmd) where T : new();
T ExecuteReader<T>(string SQL) where T : new();
int ExecuteTrans(DbTransaction trans, string SQL);
void Close(DbConnection connection);
}
// 来源:C/S 框架网 (www.csframework.com) QQ:23404761
/// <summary>
/// ADO (ActiveX Data Object) ADO.NET 活动数据对象工厂
/// </summary>
public interface IAdoFactory
{
string BuildConnectionString(string server, int port, string dbName, string uid, string pwd, int timeout = 10);
CommandHelper CreateSqlProc(string spName);
CommandHelper CreateCommand(string commandText);
/// <summary>
/// DbCommand 的参数符号,@,?
/// </summary>
string ParamSymboName { get; }
DbParameter CreateTimestampParameter(string parameterName, string fieldName);
DbConnection CreateConnection();
DbConnection CreateConnection(string connectionString);
DbCommand CreateCommand(string commandText, CommandType commandType);
DbParameter CreateParameter(string parameterName, object parameterValue);
DbCommandBuilder CreateCommandBuilder();
DbDataAdapter CreateDataAdapter();
DbDataAdapter CreateDataAdapter(AdapterRowUpdatingEvent eventHandler);
}
// 来源:C/S 框架网 (www.csframework.com) QQ:23404761
/// <summary>
/// 数据库对象工厂
/// </summary>
public static class DatabaseFactory
{
/// <summary>
/// 创建数据库(使用 DataProviderFactory 创建数据存取基础组件,通用版)
/// </summary>
/// <param name="providerType"> 数据库类型 </param>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateMDB(DatabaseType providerType, string connectionString)
{
return new DatabaseMDB(providerType, connectionString);
}
/// <summary>
/// 创建 SQL Server 数据库
/// </summary>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateSqlDatabase(string connectionString)
{
return new DatabaseMSSQL(connectionString);
}
/// <summary>
/// 创建 MySQL 数据库
/// </summary>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateMySqlDatabase(string connectionString)
{
return new DatabaseMySQL(connectionString);
}
/// <summary>
/// 创建 Oracle 数据库
/// </summary>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateOracleDatabase(string connectionString)
{
return new DatabaseOracle(connectionString);
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="dbType"> 数据库类型 </param>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateDatabase(DatabaseType dbType, string connectionString)
{
if (dbType == DatabaseType.SqlServer)
return new DatabaseMSSQL(connectionString);
if (dbType == DatabaseType.MySQL)
return new DatabaseMySQL(connectionString);
if (dbType == DatabaseType.Oracle)
return new DatabaseOracle(connectionString);
throw new Exception ("未支持的数据库类型!");
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="dbType"> 数据库类型 </param>
/// <param name="connectionString"> 数据库连接字符串 </param>
/// <returns></returns>
public static IDatabase CreateDatabase(string dbType, string connectionString)
{
DatabaseType databaseType;
if (!Enum.TryParse<DatabaseType>(dbType, true, out databaseType))
throw new Exception ("不支持的数据库类型!");
if (databaseType == DatabaseType.SqlServer)
return new DatabaseMSSQL(connectionString);
if (databaseType == DatabaseType.MySQL)
return new DatabaseMySQL(connectionString);
if (databaseType == DatabaseType.Oracle)
return new DatabaseOracle(connectionString);
throw new Exception ("未支持的数据库类型!");
}
/// <summary>
/// ADO 组件工厂
/// </summary>
/// <param name="dbType"> 数据库类型 </param>
/// <param name="connectionString"> 数据连接字符串 </param>
/// <returns></returns>
public static IAdoFactory GetADOFactory(DatabaseType dbType, string connectionString)
{
return CreateDatabase(dbType, connectionString);
}
}
// 来源:C/S 框架网 (www.csframework.com) QQ:23404761
C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
MSSQL
SQLITE
一、C# vs SQLite:
二、 C# vs MySQL:
三、 C# vs MSSQL:
四、C# vs Oracle:
附、类型转换:
using System;
using System.Collections.Generic;
using System.Text; namespace vJine.Core.ORM { public class TypeConverter { public class sbyte_ /*: IConverter<sbyte>*/ { public static sbyte CONV_Q(object V) { return (sbyte)V; } } public class ushort_ /*: IConverter<ushort>*/ { public static ushort CONV_Q(object V) { return (ushort)V; } } public class uint_ /*: IConverter<uint>*/ { public static uint CONV_Q(object V) { return (uint)V; } } public class ulong_ /*: IConverter<ulong>*/ { public static ulong CONV_Q(object V) { return (ulong)V; } } public class bool_string /*: IConverter<bool>*/ { public static object CONV_I(object V) { if ((bool)V == true) { return "1"; } else { return "0"; } } public static bool CONV_Q(object V) { if ((string)V == "1") { return true; } else { return false; } } } public class sbyte_short /*: IConverter<sbyte>*/ { public static object CONV_I(object V) { return Convert.ToInt16(V); } public static sbyte CONV_Q(object V) { return Convert.ToSByte(V); } } public class byte_short /*: IConverter<byte>*/ { public static object CONV_I(object V) { return Convert.ToInt16(V); } public static byte CONV_Q(object V) { return Convert.ToByte(V); } } public class short_int /*: IConverter<short>*/ { public static object CONV_I(object V) { return Convert.ToInt32(V); } public static short CONV_Q(object V) { return Convert.ToInt16(V); } } public class ushort_int /*: IConverter<ushort>*/ { public static object CONV_I(object V, string Name) { return Convert.ToInt32(V); } public static ushort CONV_Q(object V, string Name) { return Convert.ToUInt16(V); } } public class int_long /*: IConverter<int>*/ { public static object CONV_I(object V) { return Convert.ToInt64(V); } public static int CONV_Q(object V) { return Convert.ToInt32(V); } } public class uint_long /*: IConverter<uint>*/ { public static object CONV_I(object V) { return Convert.ToInt64(V); } public static uint CONV_Q(object V) { return Convert.ToUInt32(V); } } public class long_decimal /*: IConverter<long>*/