CREATE TABLE [dbo].[Addresses](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NULL,
[City] [nvarchar](50) NULL,
[State] [nvarchar](50) NULL,
[CountryCode] [nvarchar](50) NULL,
[PostalCode] [nvarchar](50) NULL,
[Street1] [nvarchar](50) NULL,
[Street2] [nvarchar](50) NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
What would the equivalent PostgreSQL table creation script look like, based off of the previous MSSQL create script? Looking for an approach/resource that can guide me on converting MSSQL schema to PostgreSQL ...
Phil's answer is correct. As you also asked for " for an approach ", here is a very short description to convert Microsoft T-SQL:
;
(in general: replace GO
with ;
) - and no, the ;
does not go in front of the with
keyword for a common table expression [..]
(I highly recommend to never use quoted identifiers). Details about the identifier syntax are in the manual serial
or bigserial
in Postgres. That implies the data type integer
or bigint
so you only need the keyword serial
. For details see the manual nvarchar
in Postgres. All character columns use the same character set. other data types to convert (incomplete!):
SQL Server Postgres --------------------------- image bytea binary bytea varbinary(max) bytea varchar(max) text nvarchar(max) text text text ntext text bit boolean datetime timestamp smalldatetime timestamp timestamp no equivalent uniqueidentifier uuid xml xml
there are not clustered indexes so remove the clustered
attribute
with
part defines physical properties of the table that are not relevant in Postgres dbo
schema "maps" to the public
schema in Postgres. It is somewhat uncommon prefix tables with public
though unless you changed your schema search path (there is also no need to use the schema prefix for your own functions as it is required in SQL Server) Some things to watch out for:
where name = 'Arthur'
will return something different than where name = 'arthur'
. Search this site, there are plenty of questions regarding this. like '[0-9]'
) does not work in Postgres. You need to use the regex operator. where varchar_column = 2
fails in Postgres even if there are only numbers in that column In Postgres you can insert multiple null
values into a unique index on a nullable column. The following fails in SQL Server but works in Postgres
create table foo (col1 integer, col2 integer); create unique index idx_foo on foo (col1, col2); insert into foo (col1, col2) values (1, null); insert into foo (col1, col2) values (1, null);
This list is by no means complete! There is a lot more to look out when converting from one DBMS to another.
Here you go:
CREATE TABLE Addresses(
Id SERIAL NOT NULL PRIMARY KEY,
UserId int NULL,
City varchar(50) NULL,
State varchar(50) NULL,
CountryCode varchar(50) NULL,
PostalCode varchar(50) NULL,
Street1 varchar(50) NULL,
Street2 varchar(50) NULL
);
SQL Fiddle: http://sqlfiddle.com/#!15/b76b6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.