我正在使用MS-Access和JSP。我想知道如何使用自动编号字段和主键创建表。
query =“创建表Registration_A(Reg_No PRIMARY KEY AUTOINCREMENT, FName varchar(2))“;
但是它给出了语法错误。什么是正确的语法?
答案 0 :(得分:6)
CREATE TABLE Registration_A (
Reg_No AUTOINCREMENT,
FName VARCHAR(2),
CONSTRAINT RegA_PK PRIMARY KEY(Reg_No))
答案 1 :(得分:3)
您可以使用COUNTER
关键字使用DDL创建自动编号字段。我刚刚在Java控制台应用程序中对此进行了测试,它在JDBC-ODBC Bridge和UCanAccess下都适用于我:
String query =
"CREATE TABLE Registration_A (" +
"Reg_No COUNTER PRIMARY KEY, " +
"FName VARCHAR(2))";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
答案 2 :(得分:1)
此示例使用ADOX创建具有自动编号主键的访问表
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
ADOX.Key tableKey = new Key();
ADOX.Column col = new Column();
String connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\test.accdb; Jet OLEDB:Database Password=";
cat.Create(ConnString);
// Define column with AutoIncrement features
col.Name = "ID";
col.Type = ADOX.DataTypeEnum.adInteger;
col.ParentCatalog = cat;
col.Properties["AutoIncrement"].Value = true;
table.Name = "Security";
table.Columns.Append(col); // default data type is text[255]
table.Columns.Append("Username", ADOX.DataTypeEnum.adVarWChar, 255);
table.Columns.Append("Password", ADOX.DataTypeEnum.adVarWChar, 255);
table.Columns.Append("Engineer", ADOX.DataTypeEnum.adBoolean);
table.Columns.Append("Default", ADOX.DataTypeEnum.adBoolean);
// Set ID as primary key
tableKey.Name = "Primary Key";
tableKey.Columns.Append("ID");
tableKey.Type = KeyTypeEnum.adKeyPrimary;
// Add table to database
cat.Tables.Append(table);
答案 3 :(得分:1)
首先需要提及数据类型,然后是主键。
query="Create Table Registration_A (Reg_No AUTOINCREMENT PRIMARY KEY, FName varchar(2))";
答案 4 :(得分:0)
试试这个
Create Table Registration_A
(
Reg_No AUTOINCREMENT,
FName varchar(2),
PRIMARY KEY(Reg_No)
);
答案 5 :(得分:0)
七年后,在以上任何答案或任何Stack Exchange网站上的其他任何地方,我都没有看到如何在DAO中执行此操作。这是我已经解决的方法。下面的VBA代码创建一个具有自动编号字段作为主键的表,将一些任意数据放入其中,然后打开表以显示结果。我已经在Access 2007中成功运行了此代码。
Sub Make_Table_With_Autonum_Using_DAO()
Dim oDB As DAO.Database: Set oDB = CurrentDb()
Dim oTable As DAO.TableDef, sObjTable As String: sObjTable = "table_name"
Dim oField As DAO.Field, oIndex As DAO.Index
Dim oRS As DAO.Recordset
Set oTable = oDB.CreateTableDef(sObjTable)
With oTable
Set oField = .CreateField("ID_Object", dbLong) ' Create ID field.
oField.Attributes = dbAutoIncrField ' Make it autoincrement.
.Fields.Append oField ' Add to table's Fields collection.
Set oIndex = .CreateIndex("Index_Object") ' Create index.
oIndex.Primary = True ' Make it a primary key.
Set oField = oIndex.CreateField("ID_Object") ' Make index field for ID field.
oIndex.Fields.Append oField ' Add it to index's Fields coll'n.
.Indexes.Append oIndex ' Add index to table's Indexes coll'n.
Set oIndex = Nothing ' Remove index from memory.
Set oField = Nothing ' Remove field from memory.
.Fields.Append .CreateField("field2", dbText) ' Create and add other fields to
.Fields.Append .CreateField("field3", dbInteger) ' table's Fields collection.
' etc.
End With
oDB.TableDefs.Append oTable ' Add table to database's TableDefs collection.
Set oTable = Nothing
Set oRS = oDB.OpenRecordset(sObjTable) ' Enter arbitrary data into table.
oRS.AddNew: oRS!Field2 = "text 1": oRS!field3 = 123: oRS.Update
oRS.AddNew: oRS!Field2 = "text 2": oRS!field3 = 456: oRS.Update
oRS.AddNew: oRS!Field2 = "text 3": oRS!field3 = 789: oRS.Update
oRS.Close
DoCmd.OpenTable (sObjTable)
oDB.Close
Set oRS = Nothing
Set oDB = Nothing
End Sub
按照代码中出现的顺序,有关必要VBA元素的Microsoft文档为:
该文档说明了所有需要知道的内容,但并未将所有内容放在一起来解释如何使自动编号主键成为可能。以下MS文档(不再直接从MS提供)确实说明了如何设置自动编号字段,但没有说明如何使其成为主键。
在以下Microsoft社区论坛上的帖子中,Andrey Artemyev接受的答案解释了整个问题。
我上面的代码与他的回答基本相同,并附加了一些注释来解释正在发生的事情。
答案 6 :(得分:-1)
CREATE TABLE `Tablename` (Field1 AUTOINCREMENT CONSTRAINT `Primarykey` PRIMARY
KEY, `Field2` DATETIME, `Field3` TEXT(25), `Field4` DOUBLE);