Create Table in Access with identity value - database

I want to make a query in witch it creates a simple table, In SQL that would be:
CREATE TABLE RealInventry (ID int IDENTITY(1,1) NOT NULL,PartNumber varchar(255),QTY varchar(255),Locator varchar(255))
I'm trying to do the same it Access DB, but I'm getting an error in "IDENTITY" syntax. How can I make this SQL query work on Access, I'm currently using Access 2013
Any help is appreciated.

Try Autoincrement instead of Identity:
CREATE TABLE test (ID AUTOINCREMENT NOT NULL,PartNumber varchar(255),QTY varchar(255),Locator varchar(255))

Related

Is it safe to add IDENTITY PK Column to existing SQL SERVER table?

After rebuilding all of the tables in one of my SQL SERVER databases, into a new database, I failed to set the 'ID' column to IDENTITY and PRIMARY KEY for many of the tables. Most of them have data.
I discovered this T-SQL, and have successfully implemented it for a couple of the tables already. The new/replaced ID column contains the same values from the previous column (simply because they were from an auto-incremented column in the table I imported from), and my existing stored procedures all still work.
Alter Table ExistingTable
Add NewID Int Identity(1, 1)
Go
Alter Table ExistingTable Drop Column ID
Go
Exec sp_rename 'ExistingTable.NewID', 'ID', 'Column'
--Then open the table in Design View, and set the new/replaced column as the PRIMARY KEY
--I understand that I could set the PK when I create the new IDENTITY column
The new/replaced ID column is now the last column in the table, and so far, I haven't ran into issues with the ASP.Net/C# data access objects that call the stored procedures.
As mentioned, each of these tables had no PRIMARY KEY (nor FOREIGN KEY) set. With that in mind, are there any additional steps I should take to ensure the integrity of the database?
I ran across this SO post, which suggests that I should run the 'ALTER TABLE REBUILD' statement, but since there was no PK already set, do I really need to do this?
Ultimately, I just want to be sure I'm not creating issues that won't appear until later in the game, and be sure the methods I'm implementing are sound, logical, and ensure data integrity.
I suppose it might be a better option to DROP/RECREATE the table with the proper PK/IDENTITY column, and I could write some T-SQL to dump the existing data into a TEMP table, then drop/recreate, and re-populate the new table with data from the TEMP table. I specifically avoided this option as it seems much more aggressive, and I don't fully understand what it means for the Stored Procedures/Functions, etc., that depend on these tables.
Here is an example of one of the tables I've performed this on. You can see the NewID values are identical to the original ID.enter image description here
Give this a go; it's rummaged up from a script we used a few years ago in a similar situation, can't remember what version of SQLS it was used against.. If it works out for your scenario you can adapt it to your tables..
SELECT MAX(Id)+1 FROM causeCodes -- run and use value below
CREATE TABLE [dbo].[CauseCodesW]( [ID] [int] NOT NULL IDENTITY(put_maxplusone_here,1), [Code] [varchar](50) NOT NULL, [Description] [varchar](500) NULL, [IsActive] [bit] NOT NULL )
ALTER TABLE CauseCodes SWITCH TO CauseCodesW;
DROP TABLE CauseCodes;
EXEC sp_rename 'CauseCodesW','CauseCodes';
ALTER TABLE CauseCodes ADD CONSTRAINT PK_CauseCodes_Id PRIMARY KEY CLUSTERED (Id);
SELECT * FROM CauseCodes;
You can now find any tables that have FKs to this table and recreate those relationships..

SQL Server AUTO_INCREMENT error

Interface: HeidiSQL
Database: SQL Server
I'm trying to create table keeping ID (int) column as primary key as well as Auto_Increment.
But SQL Server is throwing Error 102:
Incorrect Syntax near 'Auto_increment'.
Please Help. Thanks.
The problem is that HeidiSQL will use the keyword AUTO_INCREMENT instead of IDENTITY. However AUTO_INCREMENT is not a valid specification for SQL Server CREATE TABLE statements. Here is how you do it:
CREATE TABLE "YourTableName"
(
"ID" INT NOT NULL IDENTITY(1,1)
PRIMARY KEY ("ID")
);
I didn't find a way to generate the CREATE TABLES with IDENTITY specification by using HeidiSQL. If someone does, please tell us.
In SQL Server, it's called an IDENTITY:
CREATE TABLE dbo.YourTAble
(
ID INT NOT NULL IDENTITY(1,1),
.............
)
The official MSDN documentation on CREATE TABLE contains all the possible options - that should always be your first place to look for answers like this ....
If you can use the SSMS (SQL Server Management Studio) as the GUI for database management tasks, you can change the ID column as an auto increment column by setting its Identity Specification attributes as follows
the problem is you forgot to add 'key' keyword.
the correct syntax is:
create table mytable (
first_col int primary key AUTO_INCREMENT
);
select * from mytable

Exporting tables from SQL Server into MS Access

I'm trying to export tables from SQL Server into MS Access using the data import/export feature. Everything works well but for 2 things:
Primary key constraint is not being exported to MS Access and even the identity property. Ideally I wanted the country_id column to be an AutoNumber / primary key column in MS access.
bit column is being converted to Integer in MS access. I wanted it to be a Yes/No column.
Can somebody help me with this?
This here is my SQL Server code:
CREATE TABLE country
(
id_country int IDENTITY PRIMARY KEY not null,
my_tinyint tinyint,
my_single real,
my_double float,
my_bit bit,
my_char char(7),
my_longchar text
);
You cannot create an AutoNumber field directly with DDL. The best you can do is create a Primary Key field. The following DDL query works against my 2013 Access db:
CREATE TABLE country
(
id_country LONG CONSTRAINT PK_id_country PRIMARY KEY,
my_tinyint integer,
my_single single,
my_double double,
my_bit integer,
my_char text(7),
my_longchar memo
)
This would not create an incrementing field, however. You'd have to use DAO or ADOX to handle that, or do it manually in the Access interface.Here's a SO question that shows how to do that: How to create table with Autonumber field in MS - Access at run time?

Error with Uniqueidentifier deploying database to Windows Azure

I am trying to test our site on the Windows Azure platform to see if it will work. I've worked through most of the errors, but I haven't been able to get past the following.
Element Column: [dbo].[Subscribers].[ID] has an unsupported property IsRowGuidColumn set and is not supported when used as part of a data package.
I did a lot of searching and can't find anything about this particular error. I did see some stuff about GUIDs and Azure, but nothing that's helped.
I have five tables that use a GUID/Uniqueidentifier as their primary key because they are publicly visible.
Since RowGUID column is not supported in Windows Azure SQL Database recommended alternative is to use uniqueidentifier as the column type and then use NEWID() to generate guids at insert time.
CREATE TABLE MyTable (
MyID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
Name VARCHAR(10))
INSERT INTO MyTable (name) VALUES ('string1')
INSERT INTO MyTable VALUES (newid(), 'string2')

Can you add identity to existing column in sql server 2008?

In all my searching I see that you essentially have to copy the existing table to a new table to chance to identity column for pre-2008, does this apply to 2008 also?
thanks.
most concise solution I have found so far:
CREATE TABLE Test
(
id int identity(1,1),
somecolumn varchar(10)
);
INSERT INTO Test VALUES ('Hello');
INSERT INTO Test VALUES ('World');
-- copy the table. use same schema, but no identity
CREATE TABLE Test2
(
id int NOT NULL,
somecolumn varchar(10)
);
ALTER TABLE Test SWITCH TO Test2;
-- drop the original (now empty) table
DROP TABLE Test;
-- rename new table to old table's name
EXEC sp_rename 'Test2','Test';
-- see same records
SELECT * FROM Test;
we cannot add identity to an existing column using sql command but we can do it using GUI.
Right click on the table - design - select the column on which you want to add identity.
go to the properties available below. find the identity specification and set it to yes.
save the table.
if it is not saved the go to tools from the menu - options - table designer - uncheck the checkbox prevent saving changes. now you can save the table modifications.
now your existing table had identity.
In all of the new feature documents I read about 2008, adding identity to an existing column was not a feature I recall. The solution you've found is correct and I think the process of adding identity increment to a column automatically would be only rarely useful.
Well you can do something like this.
ALTER TABLE my_table ADD ID_COLUMN INT IDENTITY (1,1) NOT NULL
You can add the IDENTITY property to an existing column using the GUI of Enterprise Manager / Management Studio.
In SQL 2005 and earlier, you could not modify an existing column to become an identity column. I deem it very very unlikely that MS changed that in 2008.

Resources