向表中插入新记录:INSERT INTO 语句
PostgreSQL INSERT INTO 语句用于向表中插入新记录
我们可以插入一行也可以同时插入多行。
语法:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
//备住:
//column1, column2,...columnN 为表中字段名。
//value1, value2, value3,...valueN 为字段对应的值。
在使用 INSERT INTO 语句时,字段列必须和数据值数量相同,且顺序也要对应。
如果我们
向表中的所有字段
插入值,则可以不需要指定字段,只需要指定插入的值即可:
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
下表列出执行插入后返回结果的说明:
实例:
mydb=# CREATE TABLE COMPANY(
mydb(# ID INT NOT NULL,
mydb(# NAME TEXT NOT NULL,
mydb(# AGE INT NOT NULL,
mydb(# ADDRESS CHAR(50),
mydb(# SALARY REAL,
mydb(# JOIN_DATE DATE,
mydb(# PRIMARY KEY(ID)
mydb(# );
CREATE TABLE
//在 COMPANY 表中插入以下数据:
mydb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1,'paul',32,'California',2000.00,'2001-07-03');
INSERT 0 1
//以下插入语句忽略 SALARY 字段:
mydb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2,'Allen',24,'Texas','2007-12-13');
INSERT 0 1
//以下插入语句 JOIN_DATE 字段使用 DEFAULT 子句来设置默认值,而不是指定值:
mydb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );
INSERT 0 1
//以下实例插入多行:
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ), (5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
INSERT 0 2
//使用 SELECT 语句查询表格数据:
mydb=# SELECT * FROM company;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
2 | Allen | 24 | Texas | | 2007-12-13
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
用于从数据库中选取数据:SELECT 语句
PostgreSQL SELECT 语句用于从数据库中选取数据。
结果被存储在一个结果表中,称为结果集。
//语法:
SELECT column1, column2,...columnN FROM table_name;
//备住:
//column1, column2,...columnN 为表中字段名。
//table_name 为表名。
//如果我们想读取表中的所有数据可以使用以下 SQL 语句:
SELECT * FROM table_name;
实例:
mydb=# SELECT * FROM company;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
2 | Allen | 24 | Texas | | 2007-12-13
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
mydb=# SELECT ID,NAME FROM company;
id | name
----+-------
1 | paul
2 | Allen
3 | Teddy
4 | Mark
5 | David
(5 行记录)
PostgreSQL 运算符
运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。
PostgreSQL 运算符是一个保留关键字或字符,一般用在 WHERE 语句中,作为过滤条件。
常见的运算符有:算术运算符、比较运算符、逻辑运算符、按位运算符。
mydb=# select 2+3;
?column?
----------
5
(1 行记录)
mydb=# select !!5;
?column?
----------
120
(1 行记录)
mydb=# SELECT * FROM company;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
2 | Allen | 24 | Texas | | 2007-12-13
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
//读取 SALARY 字段不等于 20000 的数据:
mydb=# SELECT * FROM company WHERE SALARY <> 20000;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(3 行记录)
mydb=# SELECT * FROM company;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
2 | Allen | 24 | Texas | | 2007-12-13
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
//读取 AGE 字段大于等于 25 且 SALARY 字段大于等于 6500 的数据:
mydb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 6500;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(2 行记录)
//读取 AGE 字段大于等于 25 或 SALARY 字段大于等于 6500 的数据:
mydb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 6500;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(4 行记录)
//读取 SALARY 字段不为 NULL 的数据:
mydb=# SELECT * FROM COMPANY WHERE SALARY IS NOT NULL;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | paul | 32 | California | 2000 | 2001-07-03
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(4 行记录)
位运算符
假设如果 A = 60,且 B = 13,现在以二进制格式表示,它们如下所示:
A = 0011 1100
B = 0000 1101
————————
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
下表显示了 PostgreSQL 支持的位运算符。假设变量 A 的值为 60,变量 B 的值为 13,则:
mydb=# select 60 | 13;
?column?
----------
61
(1 行记录)