Data type equivalents: MS Access Tables ↔ 'CREATE TABLE' Queries ↔ ODBC SQL

和自甴很熟 提交于 2021-02-04 14:16:38

问题


What is the correct syntax when creating a table in Access with SQL? I have tried DECIMAL, DOUBLE, NUMBER, INT... nothing lets me create an integer category with limiters.

Example:

CREATE TABLE NONGAME (
  ITEM_NUM CHAR(4) NOT NULL PRIMARY KEY,
  DESCRIPTION CHAR(30),
  ON_HAND NUMBER(4),   <------- DOES NOT WORK!
  CATEGORY CHAR(3),
  PRICE DECIMAL(6,2),  <------- DOES NOT WORK!
  ANYTHING DOUBLE(4,2) <------- DOES NOT WORK!
);

回答1:


MICROSOFT ACCESS DATA TYPES

The following table shows the Microsoft Access data types, data types used to create tables, and ODBC SQL data types. Some types have limitations, outlined following the table.

Microsoft Access data type        Data type (CREATE TABLE) ODBC SQL data type  
~~~~~~~~~~~~~~~~~~~~~~~~~~        ~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~  
BIGBINARY[1]                      LONGBINARY               SQL_LONGVARBINARY   
BINARY                            BINARY                   SQL_BINARY          
BIT                               BIT                      SQL_BIT             
COUNTER                           COUNTER                  SQL_INTEGER         
CURRENCY                          CURRENCY                 SQL_NUMERIC         
DATE/TIME                         DATETIME                 SQL_TIMESTAMP       
GUID                              GUID                     SQL_GUID            
LONG BINARY                       LONGBINARY               SQL_LONGVARBINARY   
LONG TEXT                         LONGTEXT                 SQL_LONGVARCHAR[2]  
MEMO                              LONGTEXT                 SQL_LONGVARCHAR[2]  
NUMBER (FieldSize= SINGLE)        SINGLE                   SQL_REAL            
NUMBER (FieldSize= DOUBLE)        DOUBLE                   SQL_DOUBLE          
NUMBER (FieldSize= BYTE)          UNSIGNED BYTE            SQL_TINYINT         
NUMBER (FieldSize= INTEGER)       SHORT                    SQL_SMALLINT        
NUMBER (FieldSize= LONG INTEGER)  LONG                     SQL_INTEGER         
NUMERIC                           NUMERIC                  SQL_NUMERIC         
OLE                               LONGBINARY               SQL_LONGVARBINARY   
TEXT                              VARCHAR                  SQL_VARCHAR[1]      
ARBINARY                          VARBINARY                SQL_VARBINARY       

[1] Access 4.0 applications only. Max 4000 B. Behaviour similar to LONGBINARY.
[2] ANSI applications only.
[3] Unicode and Access 4.0 applications only. 

Note: SQLGetTypeInfo returns ODBC data types. It will not return all Microsoft Access data types if more than one Microsoft Access type is mapped to the same ODBC SQL data type. All conversions in Appendix D of the ODBC Programmer's Reference are supported for the SQL data types listed in the previous table.


Limitations on Microsoft Access data types

  • BINARY**, **VARBINARY**, and **VARCHAR: Creating a BINARY, VARBINARY, or VARCHAR column of zero or unspecified length actually returns a 510-byte column.
  • BYTE: Even though a Microsoft Access NUMBER field with a FieldSize equal to BYTE is unsigned, a negative number can be inserted into the field when using the Microsoft Access driver.
  • CHAR**, **LONGVARCHAR**, and **VARCHAR: A character string literal can contain any ANSI character (1-255 decimal). Use two consecutive single quotation marks ('') to represent one single quotation mark ('). Procedures should be used to pass character data when using any special character in a character data type column.
  • DATE: Date values must be either delimited according to the ODBC canonical date format or delimited by the datetime delimiter (#). Otherwise, Microsoft Access will treat the value as an arithmetic expression and will not raise a warning or error.
    • For example, the date "March 5, 1996" must be represented as {d '1996-03-05'} or #03/05/1996#; otherwise, if only 03/05/1993 is submitted, Microsoft Access will evaluate this as 3 ÷ 5 ÷ 1996. This value rounds up to the integer 0, and since the zero day maps to 1899-12-31, this is the date used.
    • A pipe character (|) cannot be used in a date value, even if enclosed in back quotes.
  • GUID: Data type limited to Microsoft Access 4.0.
  • NUMERIC: Data type limited to Microsoft Access 4.0.

(More information at the Source)


Limitations on ODBC Desktop Driver data types

The Microsoft ODBC Desktop Database Drivers impose the following limitations on data types:

  • All data types Type conversion failures might result in the affected column being set to NULL.
  • BINARY Creating a zero-length BINARY column actually returns a 255-byte BINARY column.
  • DATE The DATE data type cannot be converted to another data type (or itself) by the CONVERT function.
  • DECIMAL (Exact Numeric)** Not supported.
  • Floating-Point Data Types The number of decimal places in a floating-point number may be limited by the number format set in the International section of the Windows Control Panel.
  • NUMERIC Supports maximum precision and a scale of 28.
  • TIMESTAMP The TIMESTAMP data type cannot be converted to itself by the CONVERT function.
  • TINYINT: TINYINT values are always unsigned.
  • Zero-Length Strings: When a dBASE, Microsoft Excel, Paradox, or Textdriver is used, inserting a zero-length string into a column actually inserts a NULL instead.

(Source)


More Information:

  • MSDN : Create and Delete Tables and Indexes Using Access SQL
  • MSDN : CREATE TABLE Statement (Microsoft Access SQL)
  • Microsoft Docs : Microsoft Access Data Types
  • Microsoft Docs : Data Type Limitations
  • Microsoft Docs : Converting between ODBC and SQL Server data types
  • Microsoft Docs : Limitations of SQL ODBC Desktop Drivers
  • Wikipedia : Open Database Connectivity (ODBC)

Small addendum by Erik:

You can actually use the Decimal data type in CREATE TABLE queries. However, this requires your statement to be executed either using ADO, or on a database that's been set to use ANSI-92 compatible syntax.

To set your database to ANSI-92 compatible syntax:

Go to File -> Options. Open the tab Object Designers. Go to Query Designer, and under SQL Server Compatible Syntax (ANSI 92), check This Database. Now you can just execute the query. Note that this affects all queries in the database, and affects queries in various ways.

To execute a query using ADO:

In the VBA Immediate Window, execute the following line:

CurrentProject.Connection.Execute "CREATE TABLE NONGAME (ITEM_NUM CHAR(4) NOT NULL PRIMARY KEY,  PRICE DECIMAL(6,2));"

Of course, you can execute more complex queries using ADO.




回答2:


DECIMAL and DOUBLE cannot be used in Access. For a "price", CURRENCY is the best bet. For my other integer, I just used NUMBER and gave it no limiters.



来源:https://stackoverflow.com/questions/50110937/data-type-equivalents-ms-access-tables-%e2%86%94-create-table-queries-%e2%86%94-odbc-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!