基本的SQL语句

创建表格:

CREATE TABLE weather (
    city            varchar(80),
    temp_lo         int,           -- low temperature
    temp_hi         int,           -- high temperature
    prcp            real,          -- precipitation
    date            date
);
CREATE TABLE 表名(
    属性名      属性的类型
);
删除表格:
DROP TABLE tablename;

DROP TABLE 表名;

想表格中插入数据:
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');

INSERT INTO 表名VALUES (属性值1, 属性值2,属性值3,属性值4,属性值5);
当然也可以如下一样
INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);
这样适合存在有的属性值是空的情况

可以利用copy语句
COPY weather FROM '/home/user/weather.txt';
COPY 表格名字 FROM '/home/user/weather.txt';

查询工具函数:
    排序:order by 表格属性名 asc(不加默认为asc即为升序排列) desc (desc为按属性值降序排列)
    分组:group by 表格属性名
聚合函数:count(数目), sum(总和),avg(均值),max(最大值), min(最小值);


外键的定义CREATE TABLE cities (
        city     varchar(80) primary key,
        location point
);

CREATE TABLE weather (
        city      varchar(80) references cities(city),
        temp_lo   int,
        temp_hi   int,
        prcp      real,
        date      date
);
向表格weather中插入数据,如果向属性ctiy中插入的数据不是表格cities中的city中的值,则这一条数据非法的
原文地址:https://www.cnblogs.com/zhangchengbing/p/5914083.html