LocalDB, table definition with comments - entity-framework

I am using Visual Studio ...if I am creating a LocalDb with the framework, everything is fine. After creating or updating an table with a comment on an column and opening table definition ...The comments I created are gone away!?
Creating:
CREATE TABLE [dbo].[Filter] (
[Id] INT IDENTITY (1, 1) NOT NULL,
-- some very usefull hint
[Name] NVARCHAR (256) NOT NULL,
-- some important hint
[Description] NVARCHAR (512) NOT NULL,
-- some very usefull hint
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Updating:
CREATE TABLE [dbo].[Filter] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (256) NOT NULL,
[Description] NVARCHAR (512) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Is it not possible to add some comments?

The database does not store comments in its schema definitions.
All you can do is store and or check in your table definition SQL script (with our without comments) along with other files of your Visual Studio project if you like to.

Related

Why is Heroku throwing me a NULL error when inserting a user into a postgresql database? [duplicate]

Using Postgres, I'm trying to use AUTO_INCREMENT to number my primary key automatically in SQL. However, it gives me an error.
CREATE TABLE Staff (
ID INTEGER NOT NULL AUTO_INCREMENT,
Name VARCHAR(40) NOT NULL,
PRIMARY KEY (ID)
);
The error:
********** Error **********
ERROR: syntax error at or near "AUTO_INCREMENT"
SQL state: 42601
Character: 63
Any idea why?
Postgres 10 or later
(serial columns remain unchanged, see below.)
Consider a standard-SQL IDENTITY column. Can be GENERATED BY DEFAULT or (stricter) GENERATED ALWAYS.
Basics in the manual for CREATE TABLE.
Details in this blog entry by its principal author Peter Eisentraut.
Create table with IDENTITY column
CREATE TABLE staff (
staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY
, staff text NOT NULL
);
Add IDENTITY column to existing table
Table may or may not be populated with rows.
ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY;
To also make it the PK at the same time (table can't have a PK yet):
ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
See:
How to add a PostgreSQL 10 identity column to an existing table with rows?
Replace serial with IDENTITY column
See:
How to change a table ID from serial to identity?
You can override system values or user input in INSERT commands with OVERRIDING {SYSTEM|USER} VALUE.
Postgres 9.6 or older
(Still supported in newer versions, too.)
Use the serial pseudo data type:
CREATE TABLE staff (
staff_id serial PRIMARY KEY,
, staff text NOT NULL
);
It creates and attaches the sequence object automatically and sets the DEFAULT to nextval() from the sequence. It does all you need.
I use legal, lower-case, unquoted identifiers in my examples. Makes your life with Postgres easier.
You do not specify which RDBMS you are using, however, in SQL Server you can use this syntax:
CREATE TABLE [dbo].[Staff]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] VARCHAR(40) NOT NULL,
CONSTRAINT [ID] 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]
GO
In the SQL server database you can use Identity(1,1) like this:
CREATE TABLE Staff
(
ID INT IDENTITY(1,1) NOT NULL,
Name VARCHAR(40) NOT NULL,
PRIMARY KEY (ID)
);
PostgreSQL: If you absolutely must have your own auto increment value:
Then use a sequence:
ericlesc_schools=> drop table yar;
DROP TABLE
ericlesc_schools=> drop sequence user_id_seq;
DROP SEQUENCE
ericlesc_schools=> create sequence user_id_seq;
CREATE SEQUENCE
ericlesc_schools=> create table yar(
id int default nextval('user_id_seq'),
foobar varchar);
CREATE TABLE
ericlesc_schools=> insert into yar (foobar) values('hey alex');
INSERT 0 1
ericlesc_schools=> insert into yar (foobar) values('hey what derick');
INSERT 0 1
ericlesc_schools=> insert into yar (foobar) values('I look like a hushpuppy');
INSERT 0 1
ericlesc_schools=> select * from yar;
id | foobar
----+-----------------
1 | hey alex
2 | hey what derick
3 | I look like a hushpuppy
(3 rows)

IBM Db2 on Cloud script creating tables in the wrong schema

On IBM Db2 on Cloud I have imported a script. I created a new schema under which I want to have the new tables created, but when I run the script, it keeps trying to create the tables in a previous schema. Not sure how to get the scripts to create the tables in the new schema.
I have tried the below script without the .SQL_GROUPING_SORTING and it tries to add the tables to a different schema. I have changed the default schema in the Run SQL window within db2 to SQL_GROUPING_SORTING and am now getting the error
""KZF72118" does not have the privilege to perform operation "IMPLICIT CREATE SCHEMA".. SQLCODE=-552, SQLSTATE=42502, DRIVER=4.26.14"
DDL statement for table 'HR' database:
CREATE TABLE EMPLOYEES.SQL_GROUPING_SORTING (
EMP_ID CHAR(9) NOT NULL,
F_NAME VARCHAR(15) NOT NULL,
L_NAME VARCHAR(15) NOT NULL,
SSN CHAR(9),
B_DATE DATE,
SEX CHAR,
ADDRESS VARCHAR(30),
JOB_ID CHAR(9),
SALARY DECIMAL(10,2),
MANAGER_ID CHAR(9),
DEP_ID CHAR(9) NOT NULL,
PRIMARY KEY (EMP_ID));
CREATE TABLE JOB_HISTORY.SQL_GROUPING_SORTING (
EMPL_ID CHAR(9) NOT NULL,
START_DATE DATE,
JOBS_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9),
PRIMARY KEY (EMPL_ID,JOBS_ID));
CREATE TABLE JOBS.SQL_GROUPING_SORTING (
JOB_IDENT CHAR(9) NOT NULL,
JOB_TITLE VARCHAR(15) ,
MIN_SALARY DECIMAL(10,2),
MAX_SALARY DECIMAL(10,2),
PRIMARY KEY (JOB_IDENT));
CREATE TABLE DEPARTMENTS.SQL_GROUPING_SORTING (
DEPT_ID_DEP CHAR(9) NOT NULL,
DEP_NAME VARCHAR(15) ,
MANAGER_ID CHAR(9),
LOC_ID CHAR(9),
PRIMARY KEY (DEPT_ID_DEP));
CREATE TABLE LOCATIONS.SQL_GROUPING_SORTING (
LOCT_ID CHAR(9) NOT NULL,
DEP_ID_LOC CHAR(9) NOT NULL,
PRIMARY KEY (LOCT_ID,DEP_ID_LOC));
With the Db2 on Cloud Lite Plan
The Lite plan uses one database schema.
So the only schema you can use is the one that matches your user name. In your case this would be KZF72118
Create your tables with out a schema name, and they will be created in schema KZF72118.
You would need to use one of the other plans to remove this restriction

How to represent a value that's not null except for one row?

I'm attempting to model the entities for a resume and in doing so I came to the following issue: all except the most recently held positions have an end date.
Is there a good way to constrain at most one row allowed to be null. Event better would be that the single allowed null also had the latest start date.
Another way of saying this is can you specify a constraint that says "not null unless"?
Note: this is a learning exercise I'm doing (as opposed to working on production code).
Here's the scripted table I'm working with:
CREATE TABLE [dbo].[Employers](
[Id] [uniqueidentifier] NOT NULL,
[City] [nvarchar](max) NULL,
[State] [nvarchar](max) NULL,
[StartMonth] [int] NULL,
[StartYear] [int] NOT NULL,
[EndMonth] [int] NULL,
[EndYear] [int] NULL,
[Name] [nvarchar](max) NULL,
[Label] [nvarchar](max) NULL,
[Resume_Id] [uniqueidentifier] NULL,
[UserProfile_UserId] [int] 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] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Employers] WITH CHECK ADD CONSTRAINT [Resume_Employers] FOREIGN KEY([Resume_Id])
REFERENCES [dbo].[Resumes] ([Id])
GO
ALTER TABLE [dbo].[Employers] CHECK CONSTRAINT [Resume_Employers]
GO
ALTER TABLE [dbo].[Employers] WITH CHECK ADD CONSTRAINT [UserProfile_Employers] FOREIGN KEY([UserProfile_UserId])
REFERENCES [dbo].[UserProfile] ([UserId])
GO
ALTER TABLE [dbo].[Employers] CHECK CONSTRAINT [UserProfile_Employers]
GO
You can set a date which is certain to be an invalid one, like 31.12.9999 instead of NULL and then check it on the app side. Otherwise you can set a boolean field to show if a person has quit the position described.
I don't think there is much sense in keeping a single NULL value. If you really need it, then create a procedure to check this and call it before each insert or update operation.
P.S. you didnt't consider the case when a person has no job at the time, so all positions will have an end date.
P.P.S. why do you keep month and year as two different int fields? just use a date.

What is the acceptable method for generating an auto incrementing primary key in PostgreSQL?

Is there no easy way to do this without sequences and triggers? I have average SQL skills, and I want to use the industry standard method for pl/sql (PostgreSQL). I'm basically converting over this example table from Spring Security:
create table group_members (
id bigint generated by default as identity(start with 0) primary key,
username varchar(50) not null,
group_id bigint not null,
constraint fk_group_members_group foreign key(group_id) references groups(id));
What I have so far:
CREATE TABLE auth_group_members (
id NUMBER,
username VARCHAR(50) NOT NULL,
group_id NUMBER NOT NULL,
CONSTRAINT "FK_AuthGroupMembers" FOREIGN KEY(group_id) REFERENCES auth_groups(id)
);
The standard way would be to use serial or bigserial:
The data types serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases).
[...]
Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator.
So you'd create the table with something like this:
CREATE TABLE auth_group_members (
id bigserial primary key,
username VARCHAR(50) NOT NULL,
group_id NUMBER NOT NULL,
CONSTRAINT "FK_AuthGroupMembers" FOREIGN KEY(group_id) REFERENCES auth_groups(id)
);
The serial and bigserial types do create a sequences behind the scenes but you never have to work with the sequence directly.
In PostgreSQL 10 you can use identity columns. Here is example:
create table group_members (
id bigint generated by default as identity(start with 1) primary key,
username varchar(50) not null,
group_id bigint not null
);
Additionally:
Good article about identity columns vs serial.
PostgreSQL documentation for more info (Ctrl+F and search "AS IDENTITY").

Declaring a default constraint when creating a table

I am creating a new table in Microsoft SQL server 2000 by writing the code instead of using the GUI, I am trying to learn how to do it "the manual way".
This is the code I am actually using, and it works fine:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE())
)
I have specified the primary key, foreign key and check constraints on their own because in this way I can define a name for them, otherwise declaring them inline would make SQL Server generate a random name, and I do not "like" it.
The problem arose when I tried to declare the default value constraint: looking at the informations on the internet and how Microsoft SLQ Server Management Studio creates it, I understood that it can be created both inline and on its own:
"load_date" SMALLDATETIME NOT NULL DEFAULT GETDATE()
or
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
The inline method works fine, but it generates as usual a random name for the constaint, the stand alone method throws an error, saying Incorrect syntax near 'FOR'..
Also, if I create the table and then ALTER it, the command works:
ALTER TABLE "attachments"
ADD CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
As a reference, here is the full code I am trying to execute:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE()),
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
)
I'm totally at loss here, is what I am trying to do not possible, or I am doing something wrong?
Edit:
David M showed how to add a named default constraint using the inline syntax, I am still looking to understand if the stand alone syntax is completely wrong or it is my fault.
Do it inline with the column creation:
[load_date] SMALLDATETIME NOT NULL
CONSTRAINT [df_load_date] DEFAULT GETDATE()
I have used square brackets rather than quotes as many readers won't work with QUOTED_IDENTIFIERS on by default.