I need to make the column for store serial number of orders in the online shop.
Currently, I have this one
CREATE TABLE public.orders
(
id SERIAL PRIMARY KEY NOT NULL,
title VARCHAR(100) NOT NULL
);
CREATE UNIQUE INDEX orders_id_uindex ON public.orders (id);
But I need to create the special alphanumeric format for storing this number
like this 5CC806CF751A2.
How can I create this format with Postgres capabilities?
You can create a view that simply converts the ID to a hex value:
create view readable_orders
as
select id,
to_hex(id) as readable_id,
title
from orders;
Related
I have a primary key column in my SQL table in PostgreSQL named "id". It is a "bigseries" column. I want to convert the column to a "UUID" column. It entered the below command in the terminal:
alter table people alter column id uuid;
and
alter table people alter column id uuid using (uuid_generate_v4());
but neither of them worked.
In both tries I got the error message
ERROR: syntax error at or near "uuid"
LINE 1: alter table people alter column id uuid using (uuid_generate...
What is the correct syntax?
First of all uuid_generate_v4() is a function which is provided by an extension called uuid-ossp. You should have install that extension by using;
CREATE EXTENSION uuid-ossp;
Postgresql 13 introduced a new function which does basically the same without installing extension. The function is called gen_random_uuid()
Suppose that we have a table like the one below;
CREATE TABLE people (
id bigserial primary key,
data text
);
The bigserial is not a real type. It's a macro which basically creates bigint column with default value and a sequence. The default value is next value of that sequence.
For your use case, to change data type, you first should drop the old default value. Then, alter the type and finally add new default value expression. Here is the sample:
ALTER TABLE people
ALTER id DROP DEFAULT,
ALTER id TYPE uuid using (gen_random_uuid() /* or uuid_generate_v4() */ ),
ALTER id SET DEFAULT gen_random_uuid() /* or uuid_generate_v4() */ ;
CREATE TABLE IF NOT EXISTS people (
id uuid NOT NULL CONSTRAINT people_pkey PRIMARY KEY,
address varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255)
);
This is the correct syntax to create table in postgres SQL, it's better to do these constraints at beginning to avoid any error.
For using alter command you would do the following:
ALTER TABLE customer ADD COLUMN cid uuid PRIMARY KEY;
Most of errors that you could find while writing command either lower case or undefined correct the table name or column.
I have created one table in PL SQL Developer.
CREATE TABLE Patient_List
(
Patient_ID number NOT NULL,
Patient_Name varchar(50) NOT NULL,
Patient_Address varchar(100) NULL,
App_Date date NULL,
Descr varchar(50),
CONSTRAINT patient_pk PRIMARY KEY(Patient_ID)
);
I want to auto increment Patient_ID, I tried altering the table and modifying the Patient_ID column but it's showing an error "invalid ALTER TABLE option"
ALTER TABLE Patient_List
MODIFY Patient_ID NUMBER NOT NULL GENERATED ALWAYS AS IDENTITY;
Please help, Thanks in advance.
This is not possible.
Oracle 10g didn't even have identity columns, they were introduced in Oracle 12.1
But even with a current Oracle version, you can't convert a regular column to an identity column. You would need to add a new one.
Before identity columns, the usual way was to create a sequence and a trigger to populate the column.
See here: How to create id with AUTO_INCREMENT on Oracle?
If anybody wants to modify existing column as auto_increment use this three lines
alter table Product drop column test_id;
create sequence Product_test_id_seq INCREMENT BY 1 nocache;
alter table Product add test_id Number default Product_test_id_seq.nextval not null;
SQL> create table artwork
2 (
artwork_id number(7) NOT NULL,
barcode char(20),
title char(20),
description char(50),
PRIMARY KEY (artwork_id)
);
Table created.
SQL> select * from artwork;
no rows selected
I created the table but it showing me this error dont know. Why table it not showing?
I would expect the create to look like this:
create table artwork (
artwork_id number primary key,
barcode char(20),
title varchar2(20),
description varchar2(50)
);
Notes:
There is no need to have number(7). You can specify the length, but it is not necessary.
For title and description you definitely want varchar2(). There is no reason to store trailing spaces at the end of a name.
That may be the same for barcode, but because it might always be exactly 20 characters or trailing spaces might be significant, you might leave it as char().
The primary key constraint can be expressed in-line. There is no need for a separate declaration.
You probably simply want something like
create table artwork
(
artwork_id number(7) NOT NULL,
barcode varchar2(20),
title varchar2(20),
description varchar2(50),
PRIMARY KEY (artwork_id)
);
insert into artwork values (0, 'barcode', 'fancytitle', 'somedescription');
insert into artwork values (1, 'barcode1', 'fancytitle1', 'somedescription1');
select * from artwork;
This creates a table "ARTWORK", inserts 2 rows in it and then selects all rows currently in the table.
An empty table contains no data, with the create table-statement you only define the bucket of data, you have to fill the bucket as well with items.
I'd also recommend a auto increment column (oracle 12c) or a trigger/sequence to increment the id automatically. But that's something to read later :)
I have these three tables (I attach a preview). And of the end of list is example of data in table “virustotalscans.” There is column with name “virustotal.“ The each unique sample has number, for example 165, next sample has number 166 and etc.
TABLE VIRUTOTALS
CREATE TABLE virustotals (
virustotal INTEGER PRIMARY KEY,
virustotal_md5_hash TEXT NOT NULL,
virustotal_timestamp INTEGER NOT NULL,
virustotal_permalink TEXT NOT NULL
);
CREATE INDEX virustotals_md5_hash_idx
ON virustotals (virustotal_md5_hash);
TABLE VIRUSTOTALSCANS
CREATE TABLE virustotalscans (
virustotalscan INTEGER PRIMARY KEY,
virustotal INTEGER NOT NULL,
virustotalscan_scanner TEXT NOT NULL,
virustotalscan_result TEXT
);
CREATE INDEX virustotalscans_result_idx
ON virustotalscans (virustotalscan_result);
CREATE INDEX virustotalscans_scanner_idx
ON virustotalscans (virustotalscan_scanner);
CREATE INDEX virustotalscans_virustotal_idx
ON virustotalscans (virustotal);
TABLE DOWNLOADS
CREATE TABLE downloads (
download INTEGER PRIMARY KEY,
connection INTEGER,
download_url TEXT,
download_md5_hash TEXT
-- CONSTRAINT downloads_connection_fkey FOREIGN KEY (connection) REFERENCES connections (connection)
);
CREATE INDEX downloads_connection_idx ON downloads (connection);
CREATE INDEX downloads_md5_hash_idx
ON downloads (download_md5_hash);
CREATE INDEX downloads_url_idx
ON downloads (download_url);
Example of data in table “virustotalscans”: http://pastebin.com/7E7McZwT
Now, I need select all samples, which are on all lines in column “virustotalscan_result” empty. So I need select all samples, which don´t detect VirusTotal with any antivirus. I tried this select:
select distinct downloads.download_md5_hash from virustotalscans, virustotals,
downloads
where downloads.download_md5_hash = virustotals.virustotal_md5_hash and
virustotals.virustotal = virustotalscans.virustotal and
virustotalscans.virustotalscan_result IS NULL;
but I get MD5 hashes of all samples... Probably reason is that all samples contain at least one line, which is empty. It is logical because, some antivirus always doesn’t detect some sample.
The better example: http://pastebin.com/y81DPpmQ. Now I need select sample - number (column virustotal), where are all lines empty in column virustotalscan_result. It can be for example only number 2.
Can you help me please?
Thank you very much for replies.
SELECT download_md5_hash
FROM downloads
JOIN virustotals ON download_md5_hash = virustotal_md5_hash
WHERE virustotal IN (SELECT virustotal
FROM virustotalscans
GROUP BY virustotal
HAVING COUNT(virustotalscan_result) = 0)
Okay no seriously, if a PostgreSQL guru can help out I'm just getting started.
Basically what I want is a simple table like such:
CREATE TABLE schema.searches
(
search_id serial NOT NULL,
search_query character varying(255),
search_count integer DEFAULT 1,
CONSTRAINT pkey_search_id PRIMARY KEY (search_id)
)
WITH (
OIDS=FALSE
);
I need something like REPLACE INTO for MySQL. I don't know if I have to write my own procedure or something?
Basically:
check if the query already exists
if so, just add 1 to the count
it not, add it to the db
I can do this in my php code but I'd rather all that be done in postgres C engine
You have to add a unique constraint first.
ALTER TABLE schema.searches ADD UNIQUE (search_query);
The insert/replace command looks like this.
INSERT INTO schema.searches(search_query) VALUES ('a search query')
ON CONFLICT (search_query)
DO UPDATE SET search_count = schema.searches.search_count + 1;