I have a user table and I should remove the NOT NULL restrictions from the firstname, username and lastname fields, do I understand correctly that nothing can be changed directly, do I need to add a new sql file and with the command? How can I remove NOT NULL and break nothing? everything breaks down
CREATE TABLE "user" (
id SERIAL UNIQUE PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
last_password_reset TIMESTAMP NOT NULL DEFAULT NOW(),
roles JSONB NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
date_created TIMESTAMP NOT NULL DEFAULT NOW(),
date_updated TIMESTAMP NOT NULL DEFAULT NOW()
);
ALTER TABLE user ALTER COLUMN username DROP NOT NULL;
Related
I have two tables which look like thiS:
uploads
________
id (primary key)
user_id
file_checksum
upload_information
---------
upload_info_id (primary key)
file_checksum
metadata1
metdata2
The "many to one" relationship I am trying to enforce is this:
Many uploads can have the same file checksum
However, the file checksum can only ever point to one upload_information record, thus making the unique constraint between file_checksum and upload_info_id mandatory in the upload_information table.
I am wondering how to alter these tables in Postgres in order to achieve this relationship.
CREATE TABLE uploads (
id SERIAL NOT NULL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
user_id VARCHAR NOT NULL,
file_checksum VARCHAR NOT NULL,
);
CREATE TABLE upload_information (
upload_info_id SERIAL NOT NULL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
file_checksum VARCHAR NOT NULL,
file_name VARCHAR NOT NULL,
source_file_url VARCHAR NOT NULL,
);
Add a unique index on file_checksum.
create unique index unique_checksum on upload_information(file_checksum)
ERROR: relation "signature_level" does not exist
I'm having trouble figuring out what's the problem. Flyway is throwing me this error when migrating.
CREATE TABLE IF NOT EXISTS "user" (
id SERIAL NOT NULL PRIMARY KEY,
name text NOT NULL,
id_code numeric NOT NULL,
email text NOT NULL,
address text,
alt_contact_relation text NULL,
alt_contact_phone numeric NULL,
signature_level_id integer NULL,
username text NOT NULL,
password text NOT NULL,
create_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
update_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
status active_status NOT NULL DEFAULT 'active',
work_detail_id integer NULL,
CONSTRAINT FK_user_signature_level FOREIGN KEY (signature_level_id) REFERENCES signature_level (id)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_user_work_detail FOREIGN KEY (work_detail_id) REFERENCES work_detail (id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
This is the signature level table.
CREATE TABLE IF NOT EXISTS "signature_level" (
id SERIAL NOT NULL PRIMARY KEY,
name text NOT NULL,
create_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
update_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL
);
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.
I am new to Postgresql, I am trying to create this table actually just following a similar mysql table. But I keep getting this error for ENUM()
Below is the query for creating the table structure:
CREATE TABLE IF NOT EXISTS gkb_users (
id bigint NOT NULL,
userid character varying(50) NOT NULL DEFAULT '',
password character varying(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email character varying(100) NOT NULL DEFAULT '',
gender enum('Male','Female') NOT NULL,
dob date NOT NULL,
mobile character varying(10) NOT NULL DEFAULT '',
telephone character varying(15) NOT NULL DEFAULT '',
city character varying(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
PIN character varying(255) NOT NULL,
shipping_PIN character varying(255) NOT NULL,
area character varying(255) NOT NULL,
shipping_area character varying(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted enum('0','1') NOT NULL
);
Any help will be greatly appreciated. Thanks
ENUM is a user-defined datatype. You can use CREATE TYPE syntax to create your enum and then use it in the schema to create table.
CREATE TYPE your_enum2 AS ENUM('0','1');
CREATE TYPE your_enum1 AS ENUM('male','female');
Followed by the CREATE TABLE statement,
CREATE TABLE IF NOT EXISTS gkb_users (
id bigint NOT NULL,
userid character varying(50) NOT NULL DEFAULT '',
password character varying(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email character varying(100) NOT NULL DEFAULT '',
gender your_enum1 NOT NULL,
dob date NOT NULL,
mobile character varying(10) NOT NULL DEFAULT '',
telephone character varying(15) NOT NULL DEFAULT '',
city character varying(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
PIN character varying(255) NOT NULL,
shipping_PIN character varying(255) NOT NULL,
area character varying(255) NOT NULL,
shipping_area character varying(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted your_enum2 NOT NULL
);
Refer postgresql docs https://www.postgresql.org/docs/current/static/datatype-enum.html for more information on enum creation and usage.
You don't need an enum for this (and I personally think one never needs an enum - but that's off topic).
You should either implement this as a check constraint:
CREATE TABLE IF NOT EXISTS gkb_users
(
id bigint NOT NULL,
userid varchar(50) NOT NULL DEFAULT '',
password varchar(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email varchar(100) NOT NULL DEFAULT '',
gender text NOT NULL,
dob date NOT NULL,
mobile varchar(10) NOT NULL DEFAULT '',
telephone varchar(15) NOT NULL DEFAULT '',
city varchar(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
pin varchar(255) NOT NULL,
shipping_pin varchar(255) NOT NULL,
area varchar(255) NOT NULL,
shipping_area varchar(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted integer NOT NULL,
constraint check_gender check (gender in ('Male', 'Female')),
constraint check_deleted flag check (is_deleted in (0,1))
)
However, for is_delete should better be a proper boolean column - then you also don't need a check constraint for that column.
Postgres - like many other DBMS - is case sensitive when comparing strings. So with the above constraint you won't be able to store 'male' into the gender column.
Unrelated but: if you were assuming that varchar(255) has some magic performance benefits compared to e.g. varchar(300) then you are wrong. The maximum length of a varchar column does not influence the performance or the space requirements when storing the values.
Good day I am having problems getting this database to work using email instead of a username however it does not seem to work. a solution with an explanation would be highly appreciated. Thank you in advance
$tbl_users = "CREATE TABLE IF NOT EXISTS users (
id INT (11) NOT NULL AUTO_INCREMENT,
firstname VARCHAR (16) NOT NULL,
lastname VARCHAR (16) NOT NULL,
username VARCHAR (16) NOT NULL,
age VARCHAR (16) NOT NULL,
gender ENUM ('m','f') NOT NULL,
email VARCHAR (255) NOT NULL,
website VARCHAR (255) NULL,
country VARCHAR (255) NULL,
userlevel ENUM ('a','b','c','d') NOT NULL DEFAULT 'a',
avatar VARCHAR (255) NULL,
ip VARCHAR (255) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
notescheck DATETIME NOT NULL,
activated ENUM ('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (id),
UNIQUE KEY email
)";
$query = mysqli_query ($db_conx, $tbl_users);
if ($query === TRUE) {
echo "<h3>user table created</h3>";
} else {
echo "<h3>user table not created</h3>";
}
I think you need UNIQUE KEY (email) with ()- see documentation here and here