{"id":20479597,"url":"https://github.com/vishalvivekm/databases","last_synced_at":"2026-02-26T07:11:18.727Z","repository":{"id":261388956,"uuid":"874001528","full_name":"vishalvivekm/databases","owner":"vishalvivekm","description":"All my learning on databases","archived":false,"fork":false,"pushed_at":"2025-05-05T06:10:34.000Z","size":182,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-05T07:24:13.363Z","etag":null,"topics":["chromadb","mongodb","mysql","nosql-databases","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vishalvivekm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-10-17T04:58:19.000Z","updated_at":"2025-05-05T06:10:37.000Z","dependencies_parsed_at":"2024-11-06T10:30:52.574Z","dependency_job_id":"fa303a5a-0514-4a8a-af00-70b83760088a","html_url":"https://github.com/vishalvivekm/databases","commit_stats":null,"previous_names":["vishalvivekm/databases"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/vishalvivekm/databases","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalvivekm%2Fdatabases","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalvivekm%2Fdatabases/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalvivekm%2Fdatabases/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalvivekm%2Fdatabases/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vishalvivekm","download_url":"https://codeload.github.com/vishalvivekm/databases/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalvivekm%2Fdatabases/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279015056,"owners_count":26085643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chromadb","mongodb","mysql","nosql-databases","sql"],"created_at":"2024-11-15T15:44:47.500Z","updated_at":"2025-10-13T12:32:01.774Z","avatar_url":"https://github.com/vishalvivekm.png","language":null,"readme":"```text\nA database is a collection of data that is specifically organized for rapid search and retrieval processed by a computer.\n- Structured data for fast retrieval by a computer\n\nWith databases we can:\n- Store, retrieve and update data;\n- Get metadata;\n- Access a database remotely;\n- Restrict accesses to data;\n- Make concurrent updates;\n- Recover to some point in time;\n- Check the rules for data consistency automatically.\n\nDatabases provide schemas and metadata that allow for a quick search of the data needed.\nA schema describes how the data is organized, whereas Metadata holds structural and statistical information.\n\n```\n\n\u003cdetails\u003e\u003csummary\u003ediff between a database and a usual file\u003c/summary\u003e\n  \n```text\nfile may be structured or not, but a database must have a specific structure.\ne.g. a file with a to-do list, one can say that this file has some structure but from comp's perspective,\nit's just a plain text file until a prgrm is written that manages the data in it.\n```\n\u003c/details\u003e\n\n\nDBMS ( DataBase Management System) - \n- a type of software that allows users to define, create and control data.\n- It is a mediator between the user and the database, which means that users can work with it (database) through the interface of the DBMS. In addition, it can also help isolate data from the users.\n- the DBMS can optimize queries and retrieve data from а database in an optimal way\n\n\u003e [!Note]\n\u003e - Usually, the information in databases is compressed and stored as binaries rather than plain text that computer can easily understand. \u003cbr\u003e\n\u003e - DBMS allows comps to RW the data correctly. \u003cbr\u003e\n\u003e - DBMS controls the data in a DB\n\n- Almost all relational databases use the SQL standard, so same commands can be applied in different DBMSs.\n### Features\n\n    ACCESS to Data :-\n    if stored locally, as the files grow in number, we won't be able to find information quickly. \n    Databases provide schemas and metadata that allow for a quick search of the data needed.\n\n\n    Data consistency :-\n    One of the best features of databases is their ability to keep and restore data correctly.\n\n    Simultaneous access :- \n    databases isolate different users and can be configured to resolve conflicts between their updates.\n\n    Backup and recovery\n    DBMS allows tomake backups and then restore the data, whereas if a usual file becomes corrupted and cannot be opened, the data is gone forever. \n\nRDM - Relational Data Model \nModern databases are designed on the basis of an abstract mathematical model called RDM i.e, Postgress, MySQL, Oracle etc.\u003cbr\u003e\nTerms :-\n1. Relation -  2-D table that represents some entity or relationships\n2. entity - an abstraction of some object ( like a class in oop) i.e, a student, a car or a building, each row in the table is an object or entities that has a particular set of props or attributes\n3. A Relationship - defines how entities are inerconnected. \ne.g, each student studies in one of the groups, such a relationship in Relational DB is called many-to-one relationship.\n\nA Relation consists of 1. Attributes 2. Tuples\n1. Attribute - a column in the table: \ne.g, in reln to a stud, the attributes are : last name, first name, dob and stud number\n\n\n2. Tuple - a row in a table, a collection of specific attribute values:\nfor a student reln with the attributes student number, last name, first name, dob, the tuple can be `2140124, Carl, Johnson, 01.01.2000`\nA relation with its attributes can be briefly denoted as follows : \nStudent ( student number, last name, first name, date of birth). \n\n\n\n| student number | last name | first name | dob\n| -------------- | --------- | ---------- |  ---------- |\n|  2140124\t     | Johnson   | Carl       | 01.01.2003  |\n\n\u003e [!IMPORTANT]  \n\u003e Restrictions\n\u003e The following restrictions are imposed on the relations in the relational data model:\n\u003e 1. There cannot be two relations with the same name in a database.\n\u003e 2. All attributes within the same relation should have different names. i,e, columns with different names\n\u003e 3. There should only be one value in each cell.\n\u003e 4. The order of the rows and columns in the relations is not important.\n\n#### Integrity\n1. Entity Integrity\n2. Referential Integrity\n\n```\nEntity Integrity :- means there can't be two identical tuples (rows) in table. we can't have two absolutely identical objs, i.e, there are no identical students, they are all distinct ( PK - student number)\n- To maintain the integrity of an entity, the concept of a primary key (PK) is introduced.\n```\n\n```\nReferential Integrity :-\n- occurs when we have two connected relations.\n- The relationship of the relation is provided using a foreign key (FK). a FK is an attribute of a relation ( subordinate/dependent) that points to the PK of another relation( main /master)\n```\n\nIdentifying relationships exist when the primary key of the parent entity is included in the primary key of the child entity. On the other hand, a non-identifying relationship exists when the primary key of the parent entity is included in the child entity but not as part of the child entity’s primary key.\n- stackoverflow: https://stackoverflow.com/questions/2814469/still-confused-about-identifying-vs-non-identifying-relationships/2814663#2814663\n\n\u003e SKIPPED SOME PART OF second theory to write down here will visit later and make notes then 1-1, 1-M, M-M\n\nSQL (Structured Query Language) - a domain-specific-prgrming lang designed to handle data in tables. SQL is the standard data manipulation language used by data-driven companies around the world.\n\nSQL -\n- used to extract and update data STRUCTERED as tables, Excel spreadsheets etc,\n- is intended for use with tables that have a particular structure:\n\n\u003e sql is a prgming lang with a large set of data processing features,it's Declarative - meaning a query written in SQL is a query that tells the system what shud be done or evaluated but doesn't specify how\n\n```sql\nSELECT * FROM Students;\n```\n \nthe above query extracts all rows and columns form the table Students.\n`*` symbol is used to select all the columns form the table\n`;` is statement terminator. each sql statement ends with this else error. can be omitted only if we make one query at a time. but good practice - always put ; at the end of the query\n- Keyword such as SELECT arent case sensitive in sql lang, often written in uppercase for more visibility\n\nL in SQL: \n\" select everything from the Student\". SQL was designed to be as similar as possible to a natural language.\nIts declarative nature helps to hide the operation's complexities, letting the user define what is required in a relatively straightforward way. The system then analyses the query, chooses the control flow, and executes it.\nSQL was originally adopted as a standard by the `American National Standards Institute (ANSI)` in the 1980s\n\n### Basic Data Types\nSQL is a lang used for working with different types of data organized into a table.\nusually, data values from the same column in a table have the same meaning and type. below is the Car table\n\n\n| manufacture_year | color | horsepower | price  | electricity |\n|------------------|-------|------------|--------|-------------|\n| 2018             | Red   | 283        | 34.990 | TRUE        |\n| 2052             | Blue  | 313        | 52.000 | False       |\n\n\t\nSQL dbs usually require that each column in a database table has a name and a data type, which decides the values that be stored in the column and the operation.\nBasic subset of data types: \n- INTEGER\n- FLOAT\n- DECIMAL\n- VARCHAR\n- BOOLEAN\n\nINTEGER : numeric data type some range of mathematical integers ( -2147483648 to +2147483647) i,e, for counters etc.\n\nDecimal:  numbers for body temp, SQL supports a special data type for such values -  DECIMAL(precision, scale) \u003cbr\u003e\nThis type has two parameters : precision and scale \u003cbr\u003e\nScale : is the count of digits to the right of the decimal point \u003cbr\u003e\nPrecision : is the total count of digit in the number.\n\nThe FLOAT data type :\n- Approximate numeric data type used for floating-point numbers. \n- With it we can store very large or very small numbers. \n- Also used for calculations that require fast processing. \n- has an optional parameter n that specifies the precision and storage size (from 1 to 53).\n\n- REAL data type: FLOAT(24) ||  FLOAT of certain accuracy.\n\nText :- VARCHAR(n) - a string of symbols of varying length \u003c= n can fit\na column with type VARCHAR(5) - Vivek, plum, can be inserted not GitHub as it exceeds the length restriction and system will either truncate them or generate an error if one tries to insert such long values\n\nBoolean - TRUE or FALSE\n\n\u003cdetails\u003e\u003csummary\u003eDiscussion on saving money in sql dataabse\u003c/summary\u003e\n\t\n\tprecision and scale to use for representing money in an SQL database: https://stackoverflow.com/questions/224462/storing-money-in-a-decimal-column-what-precision-and-scale \n\t\u003e general consensus:  DECIMAL(19, 4) for accounting purposes\n\n\u003c/details\u003e\n\n\nan SQL query that defines a table friends with five columns: id of type INTEGER, name of type VARCHAR(20), birth_place_latitude of type REAL, year_income of type DECIMAL(20,2), and is_funny of type BOOLEAN.\n\n```sql\nCREATE TABLE friends (\n    id INTEGER,\n    name VARCHAR(20),\n    birth_place_latitude REAL,\n    year_income DECIMAL(20,2),\n    is_funny BOOLEAN\n);\n```\n\nthe pattern:\n```text\nCREATE TABLE table_name (\n    column_name_1 column_t_1,\n    ..., \n    column_name_n column_t_n\n);\n```\n\n\u003e later: type casting, compound types, special types for numeric data, text, timestamps, and so on\n\nLiterals :- constant values called literals\n1. String literals - 'Postgres' or \"Meshery\" or \"Lee's birthday\" or 'Lee\"s birthday'. literals are enclosed in single or double quotes.\n\n2. Numeric literals: +ve or -ve numbers specified as integer or decimal (2.3, +34.45, -103.0)  or real values in exponential notation ( 0.4e3, -4.0e-5)\n\u003e ( +ve sign is assumed by default if not specified)\n\n- may be INTEGER, REAL or DECIMAL ( type is automatically defined by data management system based on the context)  \n-  e.g, specifying a numeric value without a decimal point, which fits the INTEGER range of values, the system will treat it as INTEGER, and otherwise as DECIMAL. \n- Numeric values specified in exponential notation are treated as REAL data.\n\n\u003e type of a literal can be directly specified using the CAST (value AS type) function.\n```sql\nSELECT\n    CAST(1 AS DECIMAL(20,3));\n# here the numeric value 1 is interpreted as DECIMAL (20,3) and 1.000 as result of query. \n# This is because 3 as the second argument says that 3 decimal places will be outputed, even if they are zeros.\n```\n\nBoolean Literals - logic truth values, TRUE and FALSE, no matter how they're specified these values are identical boolean literals\n\nHello, World! \nThe below sql code ( actually, a single query) implements it \n```sql\nSELECT 'Hello, World!' ;\n# query evaluation result : Hello, World!\n``` \nthe ^^  query declaratively states that we wanna select this string as a result. \u003cbr\u003e\nThree parts :- \n- keyword `SELECT` ( case insensitive)\n- the literal to be received\n- a semicolon `;` that defines the end of the query\n\n```sql\nSELECT literal;\n# a SQL query that extracts any literal( maybe string, numeric or boolean looks as above's, literal maybe replaced with any correctly specified constant we want)\n```\n\n### Arithmatic Expression \n\nwith SQL, we can handle arithmetic expressions. \nSQL allows to work with operators such as `+`, `-`, `*`, `/`, `%`.\n\n### Precedence\n\n```sql \n# common rule of operator precedence (below in descending ordr).\n# SQL also supports brackets to make an operator take priority over any other operation.\n# Tip: (use brackets even when not needed for code readability)\n# -2+2*2-2/2 and (-2)+(2*2)-(2/2). 2nd is much easier to read!\n\n# operators\n# -\n# unary minus that changes the sign of a value;\n             SELECT -2;\n               \u003e -2\n\n# *\n# multiplication;\n    SELECT 20*15;\n    \u003e 300\n\n# /\n# division;\n    SELECT 3/5;\n    \u003e 0.60\n# %\n# modulo that returns the remainder of integer division;\n    SELECT 18%4;\n    \u003e 2\n\n# +\n# addition\n    SELECT 30+234;\n    \u003e 264\n# -\n# subtraction\n    SELECT 3-5;\n    \u003e -2\n```\n\nnot only a literal but even an arithmetic expression can also be selected in SQL\n```sql\n SELECT expression ;\n```\ne.g, the code below evaluates the expression (2+2)*15:\n```sql\n SELECT (2+2)*15;\n\u003e 60\n#   The query evaluation result is 60.\n```\n\nBasic CREATE and DROP statement\n\n- To create a new database:\n           ```CREATE DATABASE database_name```; \n- to drop the database:\n            ```DROP DATABASE database_name;```\n            (statement deletes all the tables inside the database)\n\n- To create a new table:\n  ```sql\n  CREATE TABLE table_name (\n  column_1 column_1_type,\n  column_2 column_2_type,\n  ...., \n  column_n column_n_type\n  );\n\n  ```\n- To delete a table:\n  ```sql\n  DROP TABLE table_name;\n  # statement deletes the table itself and all information stored in it.\n  ```\n\n  ### Creating Database\n( info about uni studs in a new db) \n```sql\nCREATE DATABASE students;\n# this sql query creates the database named students\n```\n\nlet's create some tables to organized the data.\n```sql\n# creating a new table\nCREATE TABLE statement;\n```\nLet's keep working with our students database and create a table students_info that will contain four columns: student_id, name, surname and age.\nThe column student_id, will hold the unique student identifier of the INT type. The columns name and surname will have VARCHAR(30) data. The age column will hold INT values.\n```sql\nCREATE TABLE students_info ( \n    student_id INT, \n    name VARCHAR(30), \n    surname VARCHAR(30), \n    age INT\n);\n```\n\nAs a result, we have an empty table students_info:\n\n| student_id | name | surname | age |\n|------------|------|---------|-----|\n\n\n- The following SQL query drops the existing database students:\n```sql\nDROP DATABASE students; \n# dropping the database,  loses all the tables stored in it and database itself. \n```\n\n- for deleting a specific table use DELETE TABLE statement\n```sql\n# delete students_info table\nDROP TABLE students_info; \n```\n\n\u003e [!IMPORTANT]\n\u003e  While the DROP DATABASE statement deletes all the tables inside the database, DROP TABLE statement deletes the table itself and all information stored in it.\n\nBASIC SELECT STATEMENT\nSELECT 'Hello, World!'; and SELECT 2.3*4+5; // basic SELECT stmnts\n\nTUPLE -\n```sql\nSELECT 'Alice', 170, 170*0.393701;\n#  this query selects a string literal, a numeric literla and an arithmetic expresn\n```\n\nSuch a set of values (or attributes  / fields) is called a tuple (record / row). where we are specifying \u003e= 1 value seperated by `,`.\n\nAlias \nin a query, for each attribute of a tuple we can specify a name ( alias) by using the keyword AS followed by a name for the value.\nin case alias - several words/a sql keyword then put them in `\" \"`\nexample ;-\n\n```sql\nSELECT \n  'Alice' AS name, \n  170 AS height_in_centimeters, \n  170*0.393701 AS \"height in inches\"\n;\nresult - a tuple with three attributes\n\n# | name | height_in_centimeters | height in inches |\n# |------|------------------------|------------------|\n# | Alice | 170                   | 66.92917        |\n\n```\nSQL is designed to process data organized in tables. Actually, the result of the example query is also a table with column names specified in aliases and consisting of only one row.\n\n\u003e [!Note] \n\u003e sql - case insensitive but use uppercase and indentation for code readability\n```sql\nSELECT 'Bob' AS name, 160 AS \"height in centimeters\", 160*0.393701 AS \"height in inches\";\n\nSELECT \n  'Bob' AS \"name\", \n  160 AS \"height in centimeters\", \n  160*0.393701 AS \"height in inches\"\n;\n\nSELECT \n  'Bob'        AS \"name\", \n  160          AS \"height in centimeters\", \n  160*0.393701 AS \"height in inches\"\n;\n```\n\n\nthird one = easily can see no of attr, values and names\nsecond one - intermediate in terms of code readability but easier to edit than third one. can choose the code formatting style of team\n\n- Syntax of basic SELECT statement\n```sql\nSELECT val1 [AS name1], ..., valN [AS nameN];\n/* SELECT keyword, a list of values to extract with optinal aliases for them, a semicolon to indicate the end of the statement */\n```\n\n```sql\nconcat('ba', 'nana'):  'banana'\nreverse('banana'): 'ananab'\nsubstr('banana', 3, 2) : 'na'\nchar_length('banana') : 6\nreplace('banana', 'n', 'l') : balala\n```\n\n\n### ALTER: \n\n- to add a new column to the existing table: \n\n```sql\nALTER TABLE table_name\nADD COLUMN column_name DATATYPE; \n```\n\n- to delete a column from the table:\n```sql\nALTER TABLE table_name\nDROP COLUMN column_name;\n```\n\n- to change the column type:\n\n```sql\nALTER TABLE table_name \nMODIFY COLUMN column_name NEWDATATYPE;\n```\n\n- to change the column name (and, possibly, datatype), use the following template:\n\n```sql\nALTER TABLE table_name\nCHANGE old_column_name new_column_name NEWDATATYPE; \n```\n\nThe syntax may vary in different SQL dialects.\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003c/summary\u003e\n\nResources : SQLite ALTER TABLE syntax diagram and specifications:\n- https://sqlite.org/lang_altertable.html \u003cbr\u003e\n- https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_position\n\nTo practice sql\n- https://sqlbolt.com/\n- https://pgexercises.com/\n- https://www.sql-ex.ru/?Lang=1\n- https://sqlzoo.net/\n- https://www.youtube.com/playlist?list=PLSE8ODhjZXjbohkNBWQs_otTrBTrjyohi\n\n\n\u003c/details\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishalvivekm%2Fdatabases","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvishalvivekm%2Fdatabases","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishalvivekm%2Fdatabases/lists"}