MS Access Create Table with Autoincrement and default date

℡╲_俬逩灬. 提交于 2020-01-01 11:39:24

问题


I try to create MS Access Table with autoincrement ID and Default Date field, but next query always says "Syntax error in CREATE TABLE statement.":

CREATE TABLE Table1
(
    [ID] AUTOINCREMENT,
    [Email] TEXT(255),
    [ProductID] NUMBER,
    [DateCreate] DATETIME,
    [DateSend] DATETIME
);


ALTER TABLE Table1
ALTER [DateSend] DATETIME DEFAULT NOW() NOT NULL;

Who can help me to fix that query. Thanks!


回答1:


There are many NUMBER types in Ms-Access, so you have to be specific. I guess you want Integer.

CREATE TABLE Table1
(
    [ID] AUTOINCREMENT,
    [Email] TEXT(255),
    [ProductID] INTEGER,
    [DateCreate] DATETIME,
    [DateSend] DATETIME
);

The ALTER TABLE syntax requires ALTER COLUMN :

ALTER TABLE Table1
ALTER COLUMN
    [DateSend] DATETIME DEFAULT NOW() NOT NULL;

You could also have those two in one statement:

CREATE TABLE Table1
(
    [ID] AUTOINCREMENT,
    [Email] TEXT(255),
    [ProductID] INTEGER,
    [DateCreate] DATETIME,
    [DateSend] DATETIME DEFAULT NOW() NOT NULL
);

It's best practise to have a PRIMARY KEY on every table, and you probably intended that for the ID:

    [ID] AUTOINCREMENT PRIMARY KEY,

A page with a lot of useful information about how to handle Access with SQL:

Intermediate Microsoft Jet SQL for Access 2000




回答2:


CREATE TABLE Tblcontact
(
contactid AUTOINCREMENT PRIMARY KEY ,
firstname CHAR (60),
lastname CHAR (60),
email VARCHAR (75)
);


来源:https://stackoverflow.com/questions/6000278/ms-access-create-table-with-autoincrement-and-default-date

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