{"id":3199,"date":"2021-08-09T11:46:16","date_gmt":"2021-08-09T14:46:16","guid":{"rendered":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/?p=3199"},"modified":"2025-09-02T14:22:08","modified_gmt":"2025-09-02T17:22:08","slug":"postgressql-json-types","status":"publish","type":"post","link":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/2021\/08\/09\/postgressql-json-types","title":{"rendered":"PostgresSQL JSON types"},"content":{"rendered":"\n<p><span style=\"font-weight: 400\">Fala pessoal, no PET Reda\u00e7\u00e3o desta semana iremos abordar e entender melhor sobre uma das formas de armazenamento\u00a0 de dados do RDBMS (<\/span><i><span style=\"font-weight: 400\">Relational Database Management System<\/span><\/i><span style=\"font-weight: 400\">) <\/span><i><span style=\"font-weight: 400\">PostgreSQL<\/span><\/i><span style=\"font-weight: 400\">, o <\/span><i><span style=\"font-weight: 400\">JSON types<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">O que \u00e9 <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">?<\/span><\/h3>\n<p><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">,\u00a0 sigla para <\/span><i><span style=\"font-weight: 400\">JavaScript Object Notation<\/span><\/i><span style=\"font-weight: 400\">, \u00e9 um formato compacto e simples de troca de dados entre sistemas amplamente utilizado em diversas linguagens e tecnologias. Destaca-se pela facilidade de leitura e interpreta\u00e7\u00e3o para humanos e pelo desempenho na an\u00e1lise e gera\u00e7\u00e3o para as m\u00e1quinas. Exemplo de um <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-3188 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img1.png\" alt=\"\" width=\"485\" height=\"209\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img1.png 485w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img1-300x129.png 300w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">O conjunto desses dados que est\u00e3o dentro dos par\u00eanteses \u00e9 chamado de objeto e, como mostra a imagem, o <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 organizado na forma de atributo-valor, por exemplo, o campo <\/span><i><span style=\"font-weight: 400\">\u201cnome\u201d <\/span><\/i><span style=\"font-weight: 400\">\u00e9 um atributo e o <\/span><i><span style=\"font-weight: 400\">\u201cJo\u00e3o da Silva\u201d <\/span><\/i><span style=\"font-weight: 400\">\u00e9 o valor. Na imagem, percebe-se a utiliza\u00e7\u00e3o de valores em <\/span><i><span style=\"font-weight: 400\">strings<\/span><\/i><span style=\"font-weight: 400\"> (texto), <\/span><i><span style=\"font-weight: 400\">numbers<\/span><\/i><span style=\"font-weight: 400\"> (n\u00fameros) e <\/span><i><span style=\"font-weight: 400\">arrays<\/span><\/i><span style=\"font-weight: 400\"> (vetores), mas o <\/span><i><span style=\"font-weight: 400\">JSON <\/span><\/i><span style=\"font-weight: 400\">aceita os outros valores, como booleanos (true ou false) e outros objetos <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">, por exemplo.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Introdu\u00e7\u00e3o<\/span><span style=\"font-weight: 400\"> \u00a0 <\/span><i><span style=\"font-weight: 400\">\u00a0<\/span><\/i><\/h3>\n<p><span style=\"font-weight: 400\">Atualmente, todo mundo que vai iniciar um projeto, que necessita de um banco de dados, acaba entrando no questionamento do SQL e NoSQL. Como j\u00e1 falado anteriormente, o <\/span><i><span style=\"font-weight: 400\">PostgreSQL<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 um RDBMS, ou seja, ele pertence ao grupo dos bancos de dados relacionais (SQL), por\u00e9m a partir dos <\/span><i><span style=\"font-weight: 400\">JSON types<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 poss\u00edvel armazenar dados em um banco de dados relacional (SQL) de forma semelhante a um n\u00e3o relacional (NoSQL). Antes de entrar no assunto principal dessa reda\u00e7\u00e3o, faz-se necess\u00e1rio entender de modo geral um pouco sobre SQL e o NoSQL.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Diferen\u00e7as de SQL e NoSQL<\/span><\/h3>\n<p><b>SQL: <\/b><span style=\"font-weight: 400\">SQL \u00e9 a sigla para <\/span><i><span style=\"font-weight: 400\">\u201cStructured Query Language\u201d<\/span><\/i><span style=\"font-weight: 400\">, traduzindo ficaria &#8220;Linguagem de Consulta Estruturada\u201d, o qual tem o objetivo de tornar vi\u00e1vel o modelo relacional de banco de dados, que \u00e9 uma maneira direta de representar dados em tabelas,\u00a0 e fornecendo acesso a pontos de dados relacionados entre si.<\/span><\/p>\n<p><b>NoSQL:<\/b><b>\u00a0 <\/b><span style=\"font-weight: 400\">NoSQL significaria \u201c<\/span><i><span style=\"font-weight: 400\">Not Only SQL\u201d, traduzindo seria \u201cN\u00e3o somente SQL\u201d, criado com o objetivo de ter uma performance melhor que o modelo SQL e de forma n\u00e3o relacional, <\/span><\/i><span style=\"font-weight: 400\">e uma escalabilidade mais horizontal para suprir necessidades onde os bancos relacionais n\u00e3o s\u00e3o eficazes.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Existem 4 tipos de bancos de dados NoSQL:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><b>Documento:<\/b> <span style=\"font-weight: 400\">S\u00e3o dados no modo chave-valor, sendo armazenados como documentos. Exemplo: <\/span><i><span style=\"font-weight: 400\">JSON. <\/span><\/i><span style=\"font-weight: 400\">Um exemplo de banco de dados desse tipo seria o <\/span><i><span style=\"font-weight: 400\">MongoDB<\/span><\/i><span style=\"font-weight: 400\">;<br \/><br \/><\/span><\/li>\n<li style=\"font-weight: 400\"><b>Chave-valor:<\/b> <span style=\"font-weight: 400\">\u00c9 a que mais suporta quantidade de dados, onde um valor pode ser acessado por uma chave identificadora \u00fanica. Exemplo de banco de dados desse tipo seria o<\/span><i><span style=\"font-weight: 400\"> Riak<\/span><\/i><span style=\"font-weight: 400\">;<br \/><br \/><\/span><\/li>\n<li style=\"font-weight: 400\"><b>Colunas:<\/b> <span style=\"font-weight: 400\">S\u00e3o armazenados em linhas particulares de tabelas de disco, suportando assim, v\u00e1rias linhas e colunas e permitindo sub-colunas. Exemplo de banco de dados desse tipo seria o <\/span><i><span style=\"font-weight: 400\">Cassandra<\/span><\/i><span style=\"font-weight: 400\">;<br \/><br \/><\/span><\/li>\n<li style=\"font-weight: 400\"><b>Grafos:<\/b> <span style=\"font-weight: 400\">S\u00e3o armazenadas em forma de grafos (v\u00e9rtices e arestas). Exemplo de banco de dados desse tipo seria o<\/span><i><span style=\"font-weight: 400\"> Neo4j.<\/span><\/i><\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<p><span style=\"font-weight: 400\">Para complementar, o modelo relacional (SQL) tem como base guardar dados dentro de tabelas, diferente do modelo n\u00e3o-relacional (NoSQL), o qual tem como aplicagem o modelo <\/span><i><span style=\"font-weight: 400\">schema, <\/span><\/i><span style=\"font-weight: 400\">que \u00e9 usado uma chave de valor para recuperar valores, colunas ou documentos. Com isso, devemos levar em considera\u00e7\u00e3o a modelagem do sistema para sabermos qual das duas formas usar. Por exemplo, a escalabilidade no NoSQL \u00e9 bem maior que no SQL, ou seja, para sistemas mais complexos utilizamos o NoSQL, pois n\u00e3o compensa fazer o uso do mesmo em um sistema simples, isso geraria perda na performance.<\/span><\/p>\n<h3><span style=\"font-weight: 400\"><br \/>Introdu\u00e7\u00e3o aos <\/span><i><span style=\"font-weight: 400\">JSON types <\/span><\/i><span style=\"font-weight: 400\">do <\/span><i><span style=\"font-weight: 400\">PostgreSQL<\/span><\/i><\/h3>\n<p><span style=\"font-weight: 400\">No <\/span><i><span style=\"font-weight: 400\">PostgreSQL<\/span><\/i><span style=\"font-weight: 400\">, <\/span><i><span style=\"font-weight: 400\">JSON types<\/span><\/i><span style=\"font-weight: 400\"> s\u00e3o as formas de armazenar <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> em um banco de dados. A vantagem de guardar um <\/span><i><span style=\"font-weight: 400\">JSON <\/span><\/i><span style=\"font-weight: 400\">com essa tipagem, ao inv\u00e9s de um simples campo de texto, \u00e9 que ele ser\u00e1 validado de acordo com regras do <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">. Al\u00e9m disso, existem fun\u00e7\u00f5es e operadores espec\u00edficos para o tratamento de <\/span><i><span style=\"font-weight: 400\">JSON types<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><span style=\"font-weight: 400\">O<\/span><i><span style=\"font-weight: 400\"> Postgres<\/span><\/i><span style=\"font-weight: 400\"> possui dois tipos de dados para <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">: <\/span><i><span style=\"font-weight: 400\">json<\/span><\/i><span style=\"font-weight: 400\"> e <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><span style=\"font-weight: 400\">. Ambos s\u00e3o praticamente id\u00eanticos, mas a diferen\u00e7a est\u00e1 na forma que cada um \u00e9 armazenado. Enquanto o <\/span><i><span style=\"font-weight: 400\">json <\/span><\/i><span style=\"font-weight: 400\">\u00e9 guardado como uma c\u00f3pia exata do texto <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">, o <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 armazenado numa forma bin\u00e1ria decomposta.\u00a0<\/span><\/p>\n<h4><span style=\"font-weight: 400\"><br \/>Compara\u00e7\u00e3o <\/span><i><span style=\"font-weight: 400\">json<\/span><\/i><span style=\"font-weight: 400\"> e <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><\/h4>\n<p><span style=\"font-weight: 400\">Os principais benef\u00edcios do <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><span style=\"font-weight: 400\"> s\u00e3o:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Maior efici\u00eancia;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Consideravelmente mais r\u00e1pido para processar;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Suporta indexa\u00e7\u00e3o.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">As principais desvantagens do <\/span><i><span style=\"font-weight: 400\">jsonb <\/span><\/i><span style=\"font-weight: 400\">s\u00e3o:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Um pouco mais lento para inser\u00e7\u00e3o (por causa da convers\u00e3o);<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ocupa mais espa\u00e7o de disco em alguns casos;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Algumas consultas (principalmente as agregadas, como AVG (m\u00e9dia), COUNT (contador) e SUM (soma)) podem ser mais lentas.<\/span><\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<h3><span style=\"font-weight: 400\">Consultas com <\/span><i><span style=\"font-weight: 400\">json<\/span><\/i><span style=\"font-weight: 400\"> e <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><\/h3>\n<p><span style=\"font-weight: 400\">Para fazer consultas SQL, \u00e9 necess\u00e1rio baixar o <\/span><a href=\"https:\/\/www.postgresql.org\/download\/\"><i><span style=\"font-weight: 400\">PostgreSQL<\/span><\/i><\/a><span style=\"font-weight: 400\"> e <\/span><a href=\"https:\/\/youtu.be\/His77sqWfGU\"><span style=\"font-weight: 400\">configur\u00e1-lo<\/span><\/a><span style=\"font-weight: 400\">. Ap\u00f3s isso, pode-se iniciar a cria\u00e7\u00e3o do banco de dados e a cria\u00e7\u00e3o das tabelas com os campos <\/span><i><span style=\"font-weight: 400\">json<\/span><\/i><span style=\"font-weight: 400\"> e <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><span style=\"font-weight: 400\">. Abrindo o <\/span><i><span style=\"font-weight: 400\">pgAdmin<\/span><\/i><span style=\"font-weight: 400\"> e colocando a senha definida na configura\u00e7\u00e3o, \u00e9 poss\u00edvel criar um banco de dados dessa forma:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img decoding=\"async\" class=\"aligncenter wp-image-3189 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img2.png\" alt=\"\" width=\"896\" height=\"345\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img2.png 896w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img2-300x116.png 300w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img2-768x296.png 768w\" sizes=\"(max-width: 896px) 100vw, 896px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Seguidamente, deve-se somente nomear a <\/span><i><span style=\"font-weight: 400\">database<\/span><\/i><span style=\"font-weight: 400\"> e clicar em \u201c<\/span><i><span style=\"font-weight: 400\">save\u201d<\/span><\/i><span style=\"font-weight: 400\">. Posteriormente, para implementar as tabelas do nosso banco de dados deve-se abrir o <\/span><i><span style=\"font-weight: 400\">\u201cQuery Tool\u201d<\/span><\/i><span style=\"font-weight: 400\">, de acordo com imagem abaixo:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img decoding=\"async\" class=\"aligncenter wp-image-3190 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img3.jpg\" alt=\"\" width=\"538\" height=\"444\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img3.jpg 538w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img3-300x248.jpg 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Logo, \u00e9 necess\u00e1rio executar o seguinte <\/span><i><span style=\"font-weight: 400\">query<\/span><\/i><span style=\"font-weight: 400\"> para fazer a cria\u00e7\u00e3o da tabela \u201caluno\u201d, que vai ter um campo <\/span><i><span style=\"font-weight: 400\">id<\/span><\/i><span style=\"font-weight: 400\">, tipo serial e chave prim\u00e1ria (identificador \u00fanico de cada registro da tabela),\u00a0 e um campo <\/span><i><span style=\"font-weight: 400\">dados<\/span><\/i><span style=\"font-weight: 400\">, tipo <\/span><i><span style=\"font-weight: 400\">jsonb<\/span><\/i><span style=\"font-weight: 400\"> ou <\/span><i><span style=\"font-weight: 400\">json<\/span><\/i><span style=\"font-weight: 400\">, como na figura a seguir:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3191 size-full alignnone\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img4.png\" alt=\"\" width=\"591\" height=\"126\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img4.png 591w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img4-300x64.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Para executar a <\/span><i><span style=\"font-weight: 400\">query<\/span><\/i><span style=\"font-weight: 400\">, deve-se clicar \u201cF5\u201d no teclado. Pode-se visualizar que a tabela foi criada a partir da sequ\u00eancia:<\/span><\/p>\n<blockquote>\n<p style=\"text-align: center\"><i><span style=\"font-weight: 400\">Nome do banco de dados \u2192 Schemas \u2192 Tables<\/span><\/i><\/p>\n<\/blockquote>\n<p><span style=\"font-weight: 400\">Agora, para inserir um registro na <\/span><i><span style=\"font-weight: 400\">database<\/span><\/i><span style=\"font-weight: 400\">, \u00e9 necess\u00e1rio rodar o comando<\/span><i><span style=\"font-weight: 400\"> INSERT <\/span><\/i><span style=\"font-weight: 400\">passando um <\/span><i><span style=\"font-weight: 400\">id<\/span><\/i><span style=\"font-weight: 400\"> e depois um <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">. Ficando assim:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3192 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img5.png\" alt=\"\" width=\"643\" height=\"124\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img5.png 643w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img5-300x58.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Foi utilizado um <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> reduzido do mostrado no in\u00edcio da reda\u00e7\u00e3o, para ficar f\u00e1cil de visualizar. O <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> deve estar entre aspas simples, sem espa\u00e7amento e quebra de linha.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ap\u00f3s inserir mais alguns registros, para visualizarmos somente o nome e a idade dos alunos inseridos, por exemplo, utiliza-se uma consulta de <\/span><i><span style=\"font-weight: 400\">SELECT, <\/span><\/i><span style=\"font-weight: 400\">como essa:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3193 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img6.png\" alt=\"\" width=\"442\" height=\"123\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img6.png 442w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img6-300x83.png 300w\" sizes=\"(max-width: 442px) 100vw, 442px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Ap\u00f3s o <\/span><i><span style=\"font-weight: 400\">SELECT<\/span><\/i><span style=\"font-weight: 400\">, deve-se informar quais os dados que s\u00e3o desejados e ap\u00f3s o <\/span><i><span style=\"font-weight: 400\">FROM<\/span><\/i><span style=\"font-weight: 400\">, informa-se em qual tabela deve ser procurado. A seta depois do <\/span><i><span style=\"font-weight: 400\">dados<\/span><\/i><span style=\"font-weight: 400\"> indica qual parte do <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 requerida. Pode-se visualizar o resultado dessa consulta na figura abaixo:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3194 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img7.png\" alt=\"\" width=\"346\" height=\"145\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img7.png 346w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img7-300x126.png 300w\" sizes=\"(max-width: 346px) 100vw, 346px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">A seguir, para realizar um <\/span><i><span style=\"font-weight: 400\">SELECT<\/span><\/i><span style=\"font-weight: 400\"> filtrado pela matr\u00edcula, por exemplo, deve-se acrescentar o <\/span><i><span style=\"font-weight: 400\">WHERE<\/span><\/i><span style=\"font-weight: 400\"> na consulta e a matr\u00edcula do aluno desejado. Dessa forma:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3195 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img8.png\" alt=\"\" width=\"512\" height=\"164\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img8.png 512w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img8-300x96.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Percebe-se que ap\u00f3s informar qual a tabela que ser\u00e1 consultada \u00e9 passado o filtro pela matr\u00edcula do aluno \u201cJo\u00e3o da Silva\u201d. A matr\u00edcula deve estar entre aspas duplas e aspas simples e deve retornar isso:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3196 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img9.png\" alt=\"\" width=\"336\" height=\"96\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img9.png 336w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img9-300x86.png 300w\" sizes=\"(max-width: 336px) 100vw, 336px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Por \u00faltimo, para realizar uma altera\u00e7\u00e3o em algum campo espec\u00edfico ou todo o <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">,<\/span> <span style=\"font-weight: 400\">que j\u00e1 est\u00e3o registrados na <\/span><i><span style=\"font-weight: 400\">database<\/span><\/i><span style=\"font-weight: 400\"> \u00e9 poss\u00edvel utilizar o <\/span><i><span style=\"font-weight: 400\">UPDATE<\/span><\/i><span style=\"font-weight: 400\">. Como no exemplo abaixo:\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3197 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img10.png\" alt=\"\" width=\"562\" height=\"102\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img10.png 562w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img10-300x54.png 300w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Ap\u00f3s o <\/span><i><span style=\"font-weight: 400\">UPDATE<\/span><\/i><span style=\"font-weight: 400\">, informa-se qual a tabela que possui o registro desejado. O <\/span><i><span style=\"font-weight: 400\">SET<\/span><\/i><span style=\"font-weight: 400\"> serve para passar qual atributo ser\u00e1 alterado e qual vai ser a altera\u00e7\u00e3o. Nesse caso, trocou-se o atributo <\/span><i><span style=\"font-weight: 400\">nome,<\/span><\/i><span style=\"font-weight: 400\"> que est\u00e1 no <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\">, de \u201cJo\u00e3o da Silva\u201d para \u201cFelipe Carvalho\u201d. A fun\u00e7\u00e3o <\/span><i><span style=\"font-weight: 400\">jsonb_set<\/span><\/i><span style=\"font-weight: 400\"> recebe tr\u00eas par\u00e2metros: o campo da tabela, o atributo que deve ser alterado nesse campo e a altera\u00e7\u00e3o em si. Para o <\/span><i><span style=\"font-weight: 400\">UPDATE<\/span><\/i><span style=\"font-weight: 400\"> saber qual registro deve ser modificado \u00e9 preciso filtrar, com o <\/span><i><span style=\"font-weight: 400\">WHERE<\/span><\/i><span style=\"font-weight: 400\">, pela matr\u00edcula ou pelo nome, da mesma forma que \u00e9 feito no <\/span><i><span style=\"font-weight: 400\">SELECT<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Posteriormente, pode-se realizar o primeiro <\/span><i><span style=\"font-weight: 400\">SELECT<\/span><\/i><span style=\"font-weight: 400\"> mostrado na reda\u00e7\u00e3o novamente para visualizar todos os registros e verificar a altera\u00e7\u00e3o feita. Ficando assim:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3198 size-full\" src=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img11.png\" alt=\"\" width=\"338\" height=\"147\" srcset=\"https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img11.png 338w, https:\/\/www.ufsm.br\/app\/uploads\/sites\/791\/2021\/08\/img11-300x130.png 300w\" sizes=\"(max-width: 338px) 100vw, 338px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400\">Percebe-se que o aluno que tinha o nome \u201cJo\u00e3o da Silva\u201d agora possui o nome \u201cFelipe Carvalho\u201d.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Conclus\u00e3o<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Com isso, foi poss\u00edvel compreender mais sobre os benef\u00edcios\u00a0 dessa funcionalidade que facilita diversas situa\u00e7\u00f5es, permitindo \u201ccolocar\u201d um pouco de NoSQL dentro de um banco de dados PostgreSQL e implementar as consultas b\u00e1sicas de um sistema. Existem diversas outras formas de consultas utilizando o <\/span><i><span style=\"font-weight: 400\">JSON<\/span><\/i><span style=\"font-weight: 400\"> no PostgreSQL que encontram-se nos links das refer\u00eancias.<\/span><\/p>\n<p>\u00a0<\/p>\n<h5><span style=\"font-weight: 400\">Refer\u00eancias:<\/span><\/h5>\n<ul>\n<li><a href=\"https:\/\/www.treinaweb.com.br\/blog\/sql-vs-nosql-qual-usar\"><span style=\"font-weight: 400\">SQL e NoSQL<\/span><\/a><\/li>\n<li><a style=\"font-size: inherit\" href=\"https:\/\/www.postgresql.org\/docs\/13\/datatype-json.html\">Documenta\u00e7\u00e3o JSON Types<\/a><\/li>\n<li><a style=\"font-size: inherit\" href=\"https:\/\/www.compose.com\/articles\/faster-operations-with-the-jsonb-data-type-in-postgresql\/\">Compara\u00e7\u00e3o json e jsonb<\/a><\/li>\n<li><a style=\"font-size: inherit\" href=\"https:\/\/www.compose.com\/articles\/faster-operations-with-the-jsonb-data-type-in-postgresql\/\">Outras consultas<\/a><\/li>\n<li><a style=\"font-size: inherit\" href=\"https:\/\/blog.couchbase.com\/postgres-jsonb-and-nosql\/\">Outras consultas<\/a><\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<p style=\"text-align: right\"><span style=\"font-weight: 400\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Francisco Palermo<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fala pessoal, no PET Reda\u00e7\u00e3o desta semana iremos abordar e entender melhor sobre uma das formas de armazenamento\u00a0 de dados do RDBMS (Relational Database Management System) PostgreSQL, o JSON types. O que \u00e9 JSON? JSON,\u00a0 sigla para JavaScript Object Notation, \u00e9 um formato compacto e simples de troca de dados entre sistemas amplamente utilizado em [&hellip;]<\/p>\n","protected":false},"author":2948,"featured_media":3187,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[124,112],"tags":[196,149],"class_list":["post-3199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-destaques","category-redacao","tag-back-end-e-bd","tag-redacao"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/posts\/3199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/users\/2948"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/comments?post=3199"}],"version-history":[{"count":0,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/posts\/3199\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/media\/3187"}],"wp:attachment":[{"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/media?parent=3199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/categories?post=3199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ufsm.br\/pet\/sistemas-de-informacao\/wp-json\/wp\/v2\/tags?post=3199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}