Advertisement
Hamoudi30

Untitled

Sep 22nd, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.18 KB | None | 0 0
  1. last=# \d
  2. Did not find any relations.
  3. last=# create type genders enum('male', 'female');
  4. ERROR: syntax error at or near "enum"
  5. LINE 1: create type genders enum('male', 'female');
  6. ^
  7. last=# create type genders as enum('male', 'female');
  8. CREATE TYPE
  9. last=# create type student_info as (address text, email text);
  10. CREATE TYPE
  11. last=# create table student (name text, s_info student_info, gender genders, id serial primary key);
  12. CREATE TABLE
  13. last=# insert into student values ('mohamed', row('cairo', 'mo@y.c'), 'male');
  14. INSERT 0 1
  15. last=# insert into student values ('aya', row('alexandria', 'aya@y.c'), 'female');
  16. INSERT 0 1
  17. last=# insert into student values ('ahmed', row('tanta', 'am@y.c'), 'male');
  18. INSERT 0 1
  19. last=# alter table student add column birth date;
  20. ALTER TABLE
  21. last=# update student set birth = '1990-1-5' where id = 1;
  22. UPDATE 1
  23. last=# update student set birth = '1998-10-9' where id = 2;
  24. UPDATE 1
  25. last=# update student set birth = '2000-11-18' where id = 3;
  26. UPDATE 1
  27. last=# select * from student;
  28. name | s_info | gender | id | birth
  29. ---------+----------------------+--------+----+------------
  30. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  31. aya | (alexandria,aya@y.c) | female | 2 | 1998-10-09
  32. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  33. (3 rows)
  34.  
  35. last=# create table subject (sub_id serial primary key, name text not null, id int references student(id) on update cascade on delete set null);
  36. CREATE TABLE
  37. last=# select * from student;
  38. name | s_info | gender | id | birth
  39. ---------+----------------------+--------+----+------------
  40. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  41. aya | (alexandria,aya@y.c) | female | 2 | 1998-10-09
  42. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  43. (3 rows)
  44.  
  45. last=# isnert into student values ('aly', row(saini, 'aly@y.c'), 'male', 4, '1980-1-1');
  46. ERROR: syntax error at or near "isnert"
  47. LINE 1: isnert into student values ('aly', row(saini, 'aly@y.c'), 'm...
  48. ^
  49. last=# insert into student values ('aly', row(saini, 'aly@y.c'), 'male', 4, '1980-1-1');
  50. ERROR: column "saini" does not exist
  51. LINE 1: insert into student values ('aly', row(saini, 'aly@y.c'), 'm...
  52. ^
  53. last=# insert into student values ('aly', row('saini', 'aly@y.c'), 'male', 4, '1980-1-1');
  54. INSERT 0 1
  55. last=# insert into student values ('rewan', row('tanta', 're@y.c'), 'female', 5, '1988-1-1');
  56. INSERT 0 1
  57. last=# select * from student;
  58. name | s_info | gender | id | birth
  59. ---------+----------------------+--------+----+------------
  60. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  61. aya | (alexandria,aya@y.c) | female | 2 | 1998-10-09
  62. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  63. aly | (saini,aly@y.c) | male | 4 | 1980-01-01
  64. rewan | (tanta,re@y.c) | female | 5 | 1988-01-01
  65. (5 rows)
  66.  
  67. last=# insert into student values ('amr', row('aswan', 'amr@y.c'), 'male', 6, '2005-9-8');
  68. INSERT 0 1
  69. last=# select * from student;
  70. name | s_info | gender | id | birth
  71. ---------+----------------------+--------+----+------------
  72. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  73. aya | (alexandria,aya@y.c) | female | 2 | 1998-10-09
  74. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  75. aly | (saini,aly@y.c) | male | 4 | 1980-01-01
  76. rewan | (tanta,re@y.c) | female | 5 | 1988-01-01
  77. amr | (aswan,amr@y.c) | male | 6 | 2005-09-08
  78. (6 rows)
  79.  
  80. last=# select * from student where gender = 'male';
  81. name | s_info | gender | id | birth
  82. ---------+-----------------+--------+----+------------
  83. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  84. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  85. aly | (saini,aly@y.c) | male | 4 | 1980-01-01
  86. amr | (aswan,amr@y.c) | male | 6 | 2005-09-08
  87. (4 rows)
  88.  
  89. last=# select count('female') from student;
  90. count
  91. -------
  92. 6
  93. (1 row)
  94.  
  95. last=# select name from student where birth < '1992-10-1';
  96. name
  97. ---------
  98. mohamed
  99. aly
  100. rewan
  101. (3 rows)
  102.  
  103. last=# select name from student where birth < '1991-10-1';
  104. name
  105. ---------
  106. mohamed
  107. aly
  108. rewan
  109. (3 rows)
  110.  
  111. last=# select name from student where birth < '1991-10-1' and gender = 'male';
  112. name
  113. ---------
  114. mohamed
  115. aly
  116. (2 rows)
  117.  
  118. last=# \d
  119. List of relations
  120. Schema | Name | Type | Owner
  121. --------+--------------------+----------+----------
  122. public | student | table | postgres
  123. public | student_id_seq | sequence | postgres
  124. public | subject | table | postgres
  125. public | subject_sub_id_seq | sequence | postgres
  126. (4 rows)
  127.  
  128. last=# select * from subject;
  129. sub_id | name | id
  130. --------+------+----
  131. (0 rows)
  132.  
  133. last=# alter table subject add column max_score integer;
  134. ALTER TABLE
  135. last=# select * from subject;
  136. sub_id | name | id | max_score
  137. --------+------+----+-----------
  138. (0 rows)
  139.  
  140. last=# insert into subject (sub_id, name, id, max_score) values (1, 'cpp', 1, 80);
  141. INSERT 0 1
  142. last=# select * from subject;
  143. sub_id | name | id | max_score
  144. --------+------+----+-----------
  145. 1 | cpp | 1 | 80
  146. (1 row)
  147.  
  148. last=# insert into subject (sub_id, name, id, max_score) values (2, 'html', 2, 70);
  149. INSERT 0 1
  150. last=# select * from subject;
  151. sub_id | name | id | max_score
  152. --------+------+----+-----------
  153. 1 | cpp | 1 | 80
  154. 2 | html | 2 | 70
  155. (2 rows)
  156.  
  157. last=# insert into subject (sub_id, name, id, max_score) values (3, 'css', 3, 90);
  158. INSERT 0 1
  159. last=# insert into subject (sub_id, name, id, max_score) values (4, 'c#', 4, 50);
  160. INSERT 0 1
  161. last=# insert into subject (sub_id, name, id, max_score) values (3, 'css', 3, 90);
  162. ERROR: duplicate key value violates unique constraint "subject_pkey"
  163. DETAIL: Key (sub_id)=(3) already exists.
  164. last=# select * from subject;
  165. sub_id | name | id | max_score
  166. --------+------+----+-----------
  167. 1 | cpp | 1 | 80
  168. 2 | html | 2 | 70
  169. 3 | css | 3 | 90
  170. 4 | c# | 4 | 50
  171. (4 rows)
  172.  
  173. last=# select name, max_score from subject;
  174. name | max_score
  175. ------+-----------
  176. cpp | 80
  177. html | 70
  178. css | 90
  179. c# | 50
  180. (4 rows)
  181.  
  182. last=# select subject having max_score = MAX(max_score);
  183. ERROR: column "subject" does not exist
  184. LINE 1: select subject having max_score = MAX(max_score);
  185. ^
  186. last=# select max_score from subject having max_score = MAX(max_score);
  187. ERROR: column "subject.max_score" must appear in the GROUP BY clause or be used in an aggregate function
  188. LINE 1: select max_score from subject having max_score = MAX(max_sco...
  189. ^
  190. last=# select max_score from subject ;
  191. max_score
  192. -----------
  193. 80
  194. 70
  195. 90
  196. 50
  197. (4 rows)
  198.  
  199. last=# select max_score from subject group by name having max_score = MAX(max_score);
  200. ERROR: column "subject.max_score" must appear in the GROUP BY clause or be used in an aggregate function
  201. LINE 1: select max_score from subject group by name having max_score...
  202. ^
  203. last=# select name, max_score from subject group by name having max_score = MAX(max_score);
  204. ERROR: column "subject.max_score" must appear in the GROUP BY clause or be used in an aggregate function
  205. LINE 1: select name, max_score from subject group by name having max...
  206. ^
  207. last=# select name, (max_score) from subject group by name having max_score = MAX(max_score);
  208. ERROR: column "subject.max_score" must appear in the GROUP BY clause or be used in an aggregate function
  209. LINE 1: select name, (max_score) from subject group by name having m...
  210. ^
  211. last=# select name from subject group by name having max_score = MAX(max_score);ERROR: column "subject.max_score" must appear in the GROUP BY clause or be used in an aggregate function
  212. LINE 1: select name from subject group by name having max_score = MA...
  213. ^
  214. last=# select name, max_score from subject group by max_score having max_score = MAX(max_score);
  215. ERROR: column "subject.name" must appear in the GROUP BY clause or be used in an aggregate function
  216. LINE 1: select name, max_score from subject group by max_score havin...
  217. ^
  218. last=# select max_score from subject group by max_score having max_score = MAX(max_score);
  219. max_score
  220. -----------
  221. 70
  222. 80
  223. 90
  224. 50
  225. (4 rows)
  226.  
  227. last=# select subject where max_score = MAX(max_score);
  228. ERROR: column "subject" does not exist
  229. LINE 1: select subject where max_score = MAX(max_score);
  230. ^
  231. last=# select name from subject where max_score = MAX(max_score);
  232. ERROR: aggregate functions are not allowed in WHERE
  233. LINE 1: select name from subject where max_score = MAX(max_score);
  234. ^
  235. last=# select name from subject where max_score = select(MAX(max_score) from subject);
  236. ERROR: syntax error at or near "select"
  237. LINE 1: select name from subject where max_score = select(MAX(max_sc...
  238. ^
  239. last=# select name from subject where max_score = select(MAX(max_score) from subject);
  240. ERROR: syntax error at or near "select"
  241. LINE 1: select name from subject where max_score = select(MAX(max_sc...
  242. ^
  243. last=# select name from subject where max_score = select(MAX(max_score) from subject;);
  244. ERROR: syntax error at or near "select"
  245. LINE 1: select name from subject where max_score = select(MAX(max_sc...
  246. ^
  247. last=# select name from subject where max_score = (select MAX(max_score) from subject);
  248. name
  249. ------
  250. css
  251. (1 row)
  252.  
  253. last=# select name from, max_score subject where max_score = (select MAX(max_score) from subject);
  254. ERROR: syntax error at or near ","
  255. LINE 1: select name from, max_score subject where max_score = (selec...
  256. ^
  257. last=# select name, max_score from subject where max_score = (select MAX(max_score) from subject);
  258. name | max_score
  259. ------+-----------
  260. css | 90
  261. (1 row)
  262.  
  263. last=# select name from student where name like 'a%';
  264. name
  265. -------
  266. aya
  267. ahmed
  268. aly
  269. amr
  270. (4 rows)
  271.  
  272. last=# select count(name) from student where name = 'mohamed';
  273. count
  274. -------
  275. 1
  276. (1 row)
  277.  
  278. last=# select count(gender) from student where gender = 'male';
  279. count
  280. -------
  281. 4
  282. (1 row)
  283.  
  284. last=# select count(gender) from student where gender = 'female';
  285. count
  286. -------
  287. 2
  288. (1 row)
  289.  
  290. last=# select name, count(*) from student group by name;
  291. name | count
  292. ---------+-------
  293. rewan | 1
  294. ahmed | 1
  295. aya | 1
  296. aly | 1
  297. mohamed | 1
  298. amr | 1
  299. (6 rows)
  300.  
  301. last=# select name, count(*) from student group by name having count(*) > 2;
  302. name | count
  303. ------+-------
  304. (0 rows)
  305.  
  306. last=# insert into student values ('aly', row('saini', 'aly@y.c'), 'male', 4, '1980-1-1');
  307. ERROR: duplicate key value violates unique constraint "student_pkey"
  308. DETAIL: Key (id)=(4) already exists.
  309. last=# insert into student values ('aly', row('saini', 'aly@y.c'), 'male', 6, '1980-1-1');
  310. ERROR: duplicate key value violates unique constraint "student_pkey"
  311. DETAIL: Key (id)=(6) already exists.
  312. last=# select * from student;
  313. name | s_info | gender | id | birth
  314. ---------+----------------------+--------+----+------------
  315. mohamed | (cairo,mo@y.c) | male | 1 | 1990-01-05
  316. aya | (alexandria,aya@y.c) | female | 2 | 1998-10-09
  317. ahmed | (tanta,am@y.c) | male | 3 | 2000-11-18
  318. aly | (saini,aly@y.c) | male | 4 | 1980-01-01
  319. rewan | (tanta,re@y.c) | female | 5 | 1988-01-01
  320. amr | (aswan,amr@y.c) | male | 6 | 2005-09-08
  321. (6 rows)
  322.  
  323. last=# insert into student values ('aly', row('saini', 'aly@y.c'), 'male', 7, '1980-1-1');
  324. INSERT 0 1
  325. last=# insert into student values ('aly', row('saini', 'aly@y.c'), 'male', 8, '1980-1-1');
  326. INSERT 0 1
  327. last=# select name, count(*) from student group by name having count(*) > 2;
  328. name | count
  329. ------+-------
  330. aly | 3
  331. (1 row)
  332.  
  333. last=#
  334.  
  335.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement