Note: SQL leak filled

HAVING statement

Increase in SQL HAVING clause because, WHERE keyword can not be used with aggregate functions.
HAVING clause allows us to filter after each set of data packets.

SELECT release_year
FROM films
GROUP BY release_year
HAVING COUNT(title) > 200;

USING statement

In SQL join two tables, if the two fields are equal to the connection, may be equivalent to USING ON.

SELECT c.name AS country, continent, l.name AS language, official
FROM countries AS c
INNER JOIN languages AS l
USING(code);
SELECT c.name AS country, continent, l.name AS language, official
FROM countries AS c
INNER JOIN languages AS l
ON c.code = l.code;

SELECT INTO statement

SELECT INTO statement to select data from a table, and the data into another table.
SELECT INTO statement is used to create backup copies of tables or for archiving records.

SELECT column_name(s)
INTO new_table_name [IN externaldatabase] 
FROM old_tablename
SELECT country_code, size,
    CASE WHEN size > 50000000 THEN 'large'
        WHEN size > 1000000 THEN 'medium'
        ELSE 'small' END
        AS popsize_group
INTO pop_plus
FROM populations
WHERE year = 2015;

And operator UNION ALL UNION

UNION operation result set operator for combining two or more of the SELECT statement.

Please note that the interior of the UNION SELECT statement must have the same number of columns. The columns must also have similar data types. Meanwhile, the order of columns in each SELECT statement must be the same.

Note: By default, UNION operator to select a different value. If you allow duplicate values, use UNION ALL.

SELECT code, year
  FROM economies
	UNION ALL
SELECT country_code, year
  FROM populations
ORDER BY code, year;

INTERSECT statements

And similar UNION command, INTERSECT also the results of two SQL statements generated by the process of doing. The difference is, the UNION is a substantially OR (if the value is present in a first or second sentence, it will be selected), and is more like INTERSECT AND (the value to be present on the first one and the second sentence will be selected). UNION is union, and INTERSECT is the intersection.

SELECT code, year
  FROM economies
	INTERSECT
SELECT country_code, year
  FROM populations
ORDER BY code, year;

EXCEPT statement

EXCEPT returns two result sets difference (ie, return query from left and right to query all distinct values ​​not found).

SELECT name
  FROM cities
	EXCEPT
SELECT capital
  FROM countries
ORDER BY name;

CROSS JOIN crossconnect

Without the WHERE clause conditions, it will return the Cartesian product of the two tables are connected, the result is equal to the number of rows returned product of two table rows;

SELECT c.name AS city, l.name AS language
FROM cities AS c        
  CROSS JOIN languages AS l
WHERE c.name LIKE 'Hyder%';

semi-join 和 anti-join

When Semi-join typically occurs or exists in the use of the sql, i.e. a so-called semi-join, there are one or more matching records in the second table when two tables during association, returns a list of records; and when the difference is that ordinary join semi-join, a first table record at most once returned;

SELECT DISTINCT name
FROM languages
WHERE code IN
  (SELECT code
   FROM countries
   WHERE region = 'Middle East')
ORDER BY name;

And anti-join with the opposite semi-join, i.e. when a match is found, the second table recorded table will return to the first recording;

When not exists / not in time will be used, both will be different when dealing with null values

When selecting anti-join

    1. Not in use and there is not null constraint corresponding column
    1. not exists, does not guarantee every use anti-join
SELECT *
  FROM countries
  WHERE continent = 'Oceania'
  	AND code NOT in
  	(SELECT code
  	 FROM currencies);

Add, change, and more, delete (series of operations table)

1. New Table

CREATE TABLE table_name (
 column_a data_type,
 column_b data_type,
 column_c data_type
);

E.g:

CREATE TABLE universities(
 university_shortname text,
 university text,
 university_city text
);

2. have add-row

ALTER TABLE table_name
ADD COLUMN column_name data_type;

E.g:

ALTER TABLE professors
ADD COLUMN university_shortname text;

3. Rename the column name

ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

E.g:

ALTER TABLE affiliations
RENAME COLUMN organisation TO organization;

4. Remove Columns

ALTER TABLE table_name
DROP COLUMN column_name;

E.g:

ALTER TABLE affiliations
DROP COLUMN university_shortname;

The data migration to the new table

INSERT INTO ... 
SELECT DISTINCT ... 
FROM ...;

E.g:

INSERT INTO professors 
SELECT DISTINCT firstname, lastname, university_shortname 
FROM university_professors;

6. Delete table

DROP TABLE table_name;

E.g:

DROP TABLE university_professors;

7. Change the data type

ALTER TABLE table_name
ALTER COLUMN column_name
TYPE data_type_new;

E.g:

ALTER TABLE professors
ALTER COLUMN firstname
TYPE varchar(64);

Data casts

SELECT CAST(some_column AS data_type)
FROM table;

E.g:

SELECT transaction_date, amount + CAST(fee AS integer) AS net_amount 
FROM transactions;

Adding a unique constraint

New tables:

CREATE TABLE table_name (
 column_name UNIQUE
);

Existing table:

ALTER TABLE table_name
ADD CONSTRAINT some_name UNIQUE(column_name);

E.g:

ALTER TABLE organizations
ADD CONSTRAINT organization_unq UNIQUE(organization);

Add a primary key constraint

ALTER TABLE table_name
ADD CONSTRAINT some_name PRIMARY KEY (column_name)

E.g:

ALTER TABLE universities
ADD CONSTRAINT university_pk PRIMARY KEY(id);

Creating an auto-incremented primary key

ALTER TABLE professors 
ADD COLUMN id serial; # 新增一个类型为serial的字段

ALTER TABLE professors 
ADD CONSTRAINT professors_pkey PRIMARY KEY (id);

Merge to create a new primary key field

ALTER TABLE cars
ADD COLUMN id varchar(128);

UPDATE cars
SET id = CONCAT(make, model);

ALTER TABLE cars
ADD CONSTRAINT id_pk PRIMARY KEY(id);

Create a foreign key

ALTER TABLE a 
ADD CONSTRAINT a_fkey FOREIGN KEY (b_id) REFERENCES b (id);

E.g:

ALTER TABLE professors 
ADD CONSTRAINT professors_fkey FOREIGN KEY (university_id) REFERENCES universities (id);
Published 11 original articles · won praise 0 · Views 680

Guess you like

Origin blog.csdn.net/weixin_42871941/article/details/104873830