Crear, Modificar i Esborrar Taules En Postgres: diferència entre les revisions

De FFAWiki
Línia 76: Línia 76:
!colspan="13"|Tipus de dada
!colspan="13"|Tipus de dada
|-
|-
|style="height:100px; width:100px; text-align:center;" |Booleà
|Booleà
|style="height:100px; width:100px; text-align:center;" |colspan="6"|Numèrics
|colspan="6"|Numèrics
|style="height:100px; width:100px; text-align:center;" |colspan="3"|Text
|colspan="3"|Text
|style="height:100px; width:100px; text-align:center;" |colspan="3"|Dates i Hores
|colspan="3"|Dates i Hores
|-
|-
|style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|bool
| style="height:100px; width:100px; text-align:center;" |colspan="3"|Enters
|colspan="3"|Enters
| style="height:100px; width:100px; text-align:center;" |colspan="3"|Decimals
|colspan="3"|Decimals
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|char(n)
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|varchar(n)
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|text
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|date
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|time
| style="height:100px; width:100px; text-align:center;" |rowspan="2"|bool
|rowspan="2"|timestamp
|-
|-
| style="height:100px; width:100px; text-align:center;" |smallint
|smallint
| style="height:100px; width:100px; text-align:center;" |int
|int
| style="height:100px; width:100px; text-align:center;" |bigint
|bigint
| style="height:100px; width:100px; text-align:center;" |numeric
|numeric
| style="height:100px; width:100px; text-align:center;" |real
|real
| style="height:100px; width:100px; text-align:center;" |double precision
|double precision
|}
|}

Revisió del 17:03, 7 març 2021

Postgres

Llegenda

[] Opcional
{} Obligatoris
| Or

Crear Taula

Composició:
CREATE TABLE nom_taula (nom_camp1 tipus_dada1 [default expressió] [llista_restriccions_camp1] ,nom_camp2 tipus_dada2 [default expressió][llista_restriccions_camp2], … [llista_restricions_addicionals]);
Execució:
Ens connectem a la base de dades i...
CREATE TABLE nom_taula (id integer PRIMARY KEY , nom varchar(30) NOT NULL , curs varchar(5) NOT NULL , UNIQUE (curs));
Creem una base.

Modificar Taula

Composició:
ALTER TABLE nom_taula acció, [acció, ...]
Acció:
ADD nom_columna tipus_dada [default expressió][llista_restriccions_camp]
DROP nom_columna
ALTER nom_columna SET DEFAULT expressió
ALTER nom_columna DROP DEFAULT
ALTER nom_columna { SET | DROP } NOT NULL
ADD [ CONSTRAINT constraint_name ] { CHECK ( expressió) | UNIQUE ( nom_columna [, ... ] ) | PRIMARY KEY ( nom_columna [, ... ] ) | FOREIGN KEY ( nom_columna [, ... ] ) REFERENCES reftable( refcolumn [, ... ] ) }
Per canviar el nom:
RENAME TO nou_nom_taula;
RENAME COLUMN nom_columna TO nou_nom_columna;
Execució:
Ens connectem a la base de dades i...
ALTER TABLE nom_taula ADD nom_columna date NOT NULL ;
ALTER TABLE nom_taula ALTER nom_columna DROP NOT NULL , ADD UNIQUE (nom_columna);

Budar Taula

TRUNCATE nom_taula
Te una equivalència a:
DELETE FROM nom_taula
Esborra les dades de taula (la buida).

Esborrar Taula

DROP nom_taula
Esborra la taula (la elimina).

Tipus de Dada

Tipus de dada
Booleà Numèrics Text Dates i Hores
bool Enters Decimals char(n) varchar(n) text date time timestamp
smallint int bigint numeric real double precision