Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

sql - MS Access Create Table with Autoincrement and default date

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...