-
Notifications
You must be signed in to change notification settings - Fork 61
/
sesion1.sql
167 lines (138 loc) · 4.55 KB
/
sesion1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
CREATE DATABASE pagila;
DROP DATABASE pagila;
-- Creación de tablas
CREATE TABLE IF NOT EXISTS employees(
id INT
);
-- ver datos de una tabla
SELECT * FROM employees;
-- Tipos de datos: boolean
CREATE TABLE IF NOT EXISTS employees(
id INT,
married BOOLEAN
);
-- Insertar datos
INSERT INTO employees (id, married) VALUES (1, TRUE);
INSERT INTO employees (id, married) VALUES (2, FALSE);
-- Tipos de datos: CHAR, VARCHAR, TEXT
CREATE TABLE IF NOT EXISTS employees(
id INT,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1)
);
INSERT INTO employees (id, married, name, genre) VALUES (1, TRUE, 'Juan', 'M');
-- Tipo de datos: NUMERIC
CREATE TABLE IF NOT EXISTS employees(
id INT,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1),
salary NUMERIC(9,2)
);
INSERT INTO employees (id, married, name, genre, salary) VALUES (1, TRUE, 'Juan', 'M', 29567.23);
-- Tipo de dato: DATE
CREATE TABLE IF NOT EXISTS employees(
id INT,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE
);
INSERT INTO employees (id, married, name, genre, salary, birth_date) VALUES (1, TRUE, 'Juan', 'M', 29567.23, '1990-12-25');
-- Tipo de dato: TIME
CREATE TABLE IF NOT EXISTS employees(
id INT,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE,
start_at TIME
);
INSERT INTO employees (id, married, name, genre, salary, birth_date, start_at)
VALUES (1, TRUE, 'Juan', 'M', 29567.23, '1990-12-25', '08:30:00');
-- Identificador
CREATE TABLE IF NOT EXISTS employees(
id SERIAL,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE,
start_at TIME
);
INSERT INTO employees (married, name, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Antonio', 'M', 29567.23, '1990-12-25', '08:30:00');
-- verificar que todavía sigue permitiendo insertar un id duplicado
INSERT INTO employees (id, married, name, genre, salary, birth_date, start_at)
VALUES (1, TRUE, 'Antonio', 'M', 29567.23, '1990-12-25', '08:30:00');
-- Primary Key
CREATE TABLE IF NOT EXISTS employees(
id SERIAL PRIMARY KEY,
married BOOLEAN,
name VARCHAR(250),
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE,
start_at TIME
);
INSERT INTO employees (married, name, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Antonio', 'M', 29567.23, '1990-12-25', '08:30:00');
-- verificar que ya no permite insertar id duplicado
INSERT INTO employees (id, married, name, genre, salary, birth_date, start_at)
VALUES (1, TRUE, 'Antonio', 'M', 29567.23, '1990-12-25', '08:30:00');
-- Hacer que un campo sea obligatorio con NOT NULL
CREATE TABLE IF NOT EXISTS employees(
id SERIAL PRIMARY KEY,
married BOOLEAN,
name VARCHAR(250) NOT NULL,
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE,
start_at TIME
);
-- Comprobar que no deja insertar el empleado sin ponerle un name
INSERT INTO employees (married, genre, salary, birth_date, start_at)
VALUES (TRUE, 'M', 29567.23, '1990-12-25', '08:30:00');
-- Hacer que un campo sea único con UNIQUE
CREATE TABLE IF NOT EXISTS employees(
id SERIAL PRIMARY KEY,
married BOOLEAN,
name VARCHAR(250) NOT NULL,
email VARCHAR(100) UNIQUE,
genre CHAR(1),
salary NUMERIC(9,2),
birth_date DATE,
start_at TIME
);
INSERT INTO employees (married, name, email, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Employee1', '[email protected]', 'M', 29567.23, '1990-12-25', '08:30:00');
-- Verificar que da fallo por email repetido debería ser único
INSERT INTO employees (married, name, email, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Employee2', '[email protected]', 'M', 29567.23, '1990-12-25', '08:30:00');
SELECT * FROM employees;
-- Restricciones en rangos de datos CHECK
CREATE TABLE IF NOT EXISTS employees(
id SERIAL PRIMARY KEY,
married BOOLEAN,
name VARCHAR(250) NOT NULL,
email VARCHAR(100) UNIQUE,
genre CHAR(1),
salary NUMERIC(9,2) CHECK (salary >= 15000),
birth_date DATE CHECK (birth_date > '1975-01-01'),
start_at TIME
);
INSERT INTO employees (married, name, email, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Employee1', '[email protected]', 'M', -1, '1990-12-25', '08:30:00');
INSERT INTO employees (married, name, email, genre, salary, birth_date, start_at)
VALUES (TRUE, 'Employee1', '[email protected]', 'M', 16000, '1960-12-25', '08:30:00');
-- Renombrar tabla
ALTER TABLE IF EXISTS employees RENAME TO employees_2021;
-- Agregar columnas a las tablas
ALTER TABLE employees ADD COLUMN email VARCHAR(100);
-- Borrar columnas de una tabla
ALTER TABLE employees DROP COLUMN IF EXISTS salary;
-- Borrar tabla
DROP TABLE IF EXISTS employees;