数据库
首页 > 数据库> > PG数据库基本命令(笔记)

PG数据库基本命令(笔记)

作者:互联网

 

1、插入数据(insert 语句)

语法:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)  
VALUES (value1, value2, value3,...valueN);

实例:

INSERT INTO employees(  ID, NAME, AGE, ADDRESS, SALARY)  
VALUES
(1, 'Maxsu', 25, '海口市人民大道2880号', 109990.00 ), 
(2, 'minsu', 25, '广州中山大道 ', 125000.00 ), 
(3, '李洋', 21, '北京市朝阳区', 185000.00),   
(4, 'Manisha', 24, 'Mumbai', 65000.00), 
(5, 'Larry', 21, 'Paris', 85000.00);

 

2、查询数据(SELECT语句)

语法:

SELECT "column1", "column2"..."columnN" FROM "table_name";
SELECT * FROM "table_name";

 

3、更新数据(UPDATE语句)

语法:

UPDATE table_name  
SET column1 = value1, column2 = value2...., columnN = valueN  
WHERE [condition];

  

4、删除数据(DELETE语句)

语法:

DELETE FROM table_name  
WHERE [condition];

   实例:

DELETE FROM EMPLOYEES  
WHERE ID = 1;

 

5、ORDER BY子句

语法:

SELECT column-list  
FROM table_name  
[WHERE condition]  
[ORDER BY column1, column2, .. columnN] [ASC | DESC];

   实例:

SELECT *   
FROM EMPLOYEES  
ORDER BY AGE ASC;

 

6、分组(GROUP BY子句)

语法:

SELECT column-list  
FROM table_name  
WHERE [conditions ]  
GROUP BY column1, column2....columnN  
ORDER BY column1, column2....columnN

实例:

SELECT NAME, SUM(SALARY)   
FROM EMPLOYEES   
GROUP BY NAME;

   在上面的例子中,当我们使用GROUP BY NAME时,重复的名字数据记录被合并。 它指定GROUP BY减少冗余。

 

7、Having子句

  语法:

SELECT column1, column2  
FROM table1, table2  
WHERE [ conditions ]  
GROUP BY column1, column2  
HAVING [ conditions ]  
ORDER BY column1, column2

   实例:

  执行以下查询表“EMPLOYEES”中name字段值计数大于1的名称。

SELECT NAME,COUNT (NAME) 
FROM EMPLOYEES 
GROUP BY NAME HAVING COUNT (NAME) > 1;

   

 

 8、条件查询

  条件查询有:

  1)AND条件

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition]    
AND [search_condition];

    实例:

SELECT *  
FROM EMPLOYEES  
WHERE SALARY > 120000  
AND ID <= 4;

 

  2) OR条件

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition]    
OR [search_condition];

     实例:

SELECT *  
FROM  EMPLOYEES 
WHERE NAME = 'Minsu'  
OR ADDRESS = 'Noida';

 

  3)AND & OR条件 

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition]  AND [search_condition]     
OR [search_condition];

      实例:

SELECT *  
FROM EMPLOYEES  
WHERE (NAME = 'Minsu' AND ADDRESS = 'Delhi')  
OR (ID>= 8);

 

  4)NOT条件

     语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition] NOT [condition];

    实例:

    查询那些地址不为 NULL 的记录信息,执行以下查询:

SELECT *  
FROM EMPLOYEES  
WHERE address IS NOT NULL ;

 

     查询那些年龄不是21和24的所有记录,执行以下查询:

SELECT *  
FROM EMPLOYEES  
WHERE age NOT IN(21,24) ;

 

  5)LIKE条件

    like 与 where 子句一起,用于从指定条件满足 like 条件的表中获取数据。

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition] LIKE [condition];

    实例:

    查询名字以 Ma 开头的数据记录,如下查询语句:

SELECT *   
FROM EMPLOYEES   
WHERE NAME LIKE 'Ma%';

     执行结果如下图:

    

    查询名字以su结尾的数据记录,如下查询语句:

SELECT *   
FROM EMPLOYEES   
WHERE NAME LIKE '%su';

 

 

   6)IN条件

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition] IN [condition];

 

     实例:

    查询employee表中那些年龄为19,21的员工信息,执行以下查询:

SELECT *  
FROM EMPLOYEES  
WHERE AGE IN (19, 21);

 

 

   7)NOT IN条件

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition] NOT IN [condition];

 

 

  8)BETWEEN条件

    语法:

SELECT column1, column2, ..... columnN    
FROM table_name    
WHERE [search_condition] BETWEEN [condition];

 

    实例:

SELECT *   
FROM EMPLOYEES   
WHERE AGE BETWEEN 24 AND 27;

 

标签:name,column1,数据库,笔记,condition,column2,WHERE,SELECT,PG
来源: https://www.cnblogs.com/eason23/p/10341382.html