Database operation series-Mysql, Postgres commonly used sql statement summary

1. If I want to write a sql statement to update if it exists, or insert new data, how to solve it?

MySQL database implementation scheme: ON DUPLICATE KEY UPDATE

In the MySQL database, if the ON DUPLICATE KEY UPDATE clause is followed by the insert statement, and the row to be inserted
has a duplicate value in the unique index or primary key of the existing record in the table, then the update of the old row will occur; if If the inserted row data
is not the same as the unique index or primary key recorded in the existing table, a new record will be inserted.
In short: update if data exists, create if not

wording


INSERT INTO 表名 
	(字段名1, 字段名2 ) 
VALUES 
	(字段值1, 字段值2) 
ON DUPLICATE KEY UPDATE 
	字段名1 = VALUES(字段名1), 
	字段名2 = VALUES(字段名2)

Postgres database implementation scheme:

Precautions:

  • ON CONFLICT is only available from PostgreSQL 9.5 onwards.
  • The field to be judged must have an index constraint. For example, Unique unique index as a constraint

plan 1:

If we have this app table, now set one of the column name to unique

ALTER TABLE public.applications ADD CONSTRAINT applications_un UNIQUE (name);

Then we go to write the code

this statement: When the data exists, do nothing (DO NOTING)


const insertApp = await client.query(`INSERT INTO applications 
              (app_name, details ) 
              VALUES 
              ('${appName}', '${appDetail}') 
              ON CONFLICT ON CONSTRAINT applications_un  
              DO NOTHING;
              `);

Scenario 2:

The following statement has the same effect, the difference is that the name field is used instead of the name of the constraint


INSERT INTO customers (app_name, details)
VALUES
 (
 'AAA',
 'BBBBBB'
 ) 
ON CONFLICT (name) 
DO NOTHING;

Regarding the update: how to implement, when the app_name data exists, update the details? ?


INSERT INTO customers (app_name, details)
VALUES
 (
 'AAA',
 'BBBBBB'
 ) 
ON CONFLICT (name) 
DO
 UPDATE
   SET email = 'CCCCCCCC';
upsert

Using the same variable and executing this logic multiple times, we will find that only one is created, so it is successful~~~

2. Fuzzy query + case insensitive

Take a chestnut: query the details information and user_name information from the users table containing the letter 'xiaojin'

select * from users where lower(details) like '%xiaojin%' or lower(user_name) like '%xiaojin%' 

3. Query whether a field belongs to one of multiple values, or filter out if it is equal to one of multiple values

Take a chestnut: Query the type type from the users table as Admin or Super Admin

select * from users where "type" in ('Admin', 'Super Admin') 

4. Time range query

For example: I want to query the data in the created_at field of the users table from 2023-08-01 00:00:00.000 to 2023-08-10 00:00:00.000

select * from users where created_at between '2023-08-01 00:00:00.000' and '2023-08-10 00:00:00.000'

5. How to use dbever to batch add fields and set type in Postgres database

1. Create a new table


2. Open a sql editor

3. Write sql statements, here are some reference sentences for you

ALTER TABLE public.users ADD id serial4 NOT NULL;

ALTER TABLE public.users ADD "user_name" varchar(255) NULL;

ALTER TABLE public.users ADD description varchar(255) NULL;

ALTER TABLE public.users ADD "type" varchar(255) NULL;

ALTER TABLE public.users ADD created_at timestamp(6) NULL;

ALTER TABLE public.users ADD updated_at timestamp(6) NULL;

ALTER TABLE public.users ADD created_by varchar(255) NULL;

ALTER TABLE public.users ADD updated_by varchar(255) NULL;


4. Execute statement

5. Create table successfully


6. Some commonly used Mysql simple commands

Drop the specified database, if it exists

DROP DATABASE IF EXISTS tangdoudou;

create new database

CREATE DATABASE tangdoudou;

Enter the database tangdoudou

    USE tangdoudou;

create table

CREATE TABLE student (
  sid INT, # integer 整形

  name VARCHAR(8), # variable character可变字符

  sex  VARCHAR(1), # m->男 f->女

  score INT

);

Insert data into the table

INSERT INTO student VALUES('1','tom','F','95');

Query all data in the table

SELECT * FROM student;

change the data

UPDATE student SET name='lucy',score='100' WHERE sid='2';

delete data

DELETE FROM student WHERE sid='3';

Some commonly used chestnuts:

Discard the specified database tangdoudou, if it exists

DROP DATABASE IF EXISTS tangdoudou;

 

create a new database

CREATE DATABASE tangdoudou;

DROP DATABASE IF EXISTS tangdoudou;

Enter the database tangdoudou

USE tangdoudou;

Create a table that holds student data (number, name, gender, score);

DROP TABLE IF EXISTS student;

CREATE TABLE student (

  sid INT, # integer 整形

  name VARCHAR(8), # variable character可变字符

  sex  VARCHAR(1),# m->男 f->女

  score INT

);

Insert data into student table

INSERT INTO student VALUES('1','tom','m','85');

INSERT INTO student VALUES('2','kate','f','92');

INSERT INTO student VALUES('3','king','m','74');

Change the score of the student numbered 2 to 100, and the name is lucy;


UPDATE student SET name='lucy' WHERE sid='2';

UPDATE student SET score='100' WHERE sid='2';

Delete the student data numbered 3


DELETE FROM student WHERE sid='3';

Query the content of the student table

SELECT * FROM student;

to be added

  • That’s all for today~ Friends, ( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ See you tomorrow~~
  • Everyone be happy every day

Everyone is welcome to point out where the article needs to be corrected~
Learning is endless, cooperation is win-win

insert image description here

Welcome the little brothers and sisters passing by to put forward better opinions~~

Guess you like

Origin blog.csdn.net/tangdou369098655/article/details/132126689