Examples to count and receive duplicate objects in a list.
Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts
Friday, July 15, 2016
Monday, January 25, 2016
MySQL: varchar foreign key pitfalls
Why VACHAR as Foreign Key might be needed
Imagine that you have two tablesBOOKS & GENRES.You need to have
genre column into your BOOKS which would be one from your GENRESDepending of which data either
genre_id or genre_name u'll keep into your new column, your BOOKS table may
look like one of the following:| id *(pk) | title | author | genre_id *(fk) |
|---|---|---|---|
| 1 | The Little Prince | Saint-Exupéry | 1 |
| 2 | Fear and Loathing in Las Vegas | Hunter Thompson | 2 |
| id *(pk) | title | author | genre_type *(fk) |
|---|---|---|---|
| 1 | The Little Prince | Saint-Exupéry | FICTION |
| 2 | Fear and Loathing in Las Vegas | Hunter Thompson | ALTERNATIVE |
Wednesday, March 25, 2015
Naked JDBC in common
The JDBC™ API provides programmatic access to relational data from the Java™ programming language.
Using the JDBC API, applications can execute SQL statements, retrieve results, and propagate changes back to an underlying data source.
The JDBC API can also interact with multiple data sources in a distributed, heterogeneous environment.
Using the JDBC API, applications can execute SQL statements, retrieve results, and propagate changes back to an underlying data source.
The JDBC API can also interact with multiple data sources in a distributed, heterogeneous environment.
Monday, March 23, 2015
Transactions Control in SQL & PostgreSQL
Transactions control is the most important thing for the Data Integrity.
Transaction record is one or more related changes to the database that are made at once.
Transaction record is one or more related changes to the database that are made at once.
Properties of Transactions
- Atomicity:
ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure, and previous operations are rolled back to their former state. - Consistency:
ensures that the database properly changes states upon a successfully committed transaction. - Isolation:
enables transactions to operate independently of and transparent to each other. - Durability:
ensures that the result or effect of a committed transaction persists in case of a system failure.
Thursday, March 19, 2015
PostgreSQL Calculate Age From Birthdate
There is
AGE() function that performs two calculations depending of provided params:
- It substracts date2 from date1 if two params are provided:
--calculation: date1 - date2 age(date1, date2)
- It substracts date from the current date if only one param is provided:
--calculation: current_date - date age(date)
Monday, March 16, 2015
SQL data reliability - CONSTRAINTS usage
Data Integrity
The following categories of the data integrity exist with each RDBMS:Entity Integrity:There are no duplicate rows in a table.Domain Integrity:Enforces valid entries for a given column by restricting the type, the format, or the range of values.Referential integrity:Rows cannot be deleted, which are used by other records.User-Defined Integrity:Enforces some specific business rules that do not fall into entity, domain or referential integrity.
Constraints
Rules enforced on data columns that ensures the accuracy and reliability of the DB data.
Constrains could be column or table level.
All existing Constraint types will be described below:
NOT NULL, DEFAULT, UNIQUE, CHECK, PRIMARY KEY, FOREIGN KEY.
Constraints Naming
Ensure that each column has a descriptive name!
Doing so makes DB more self-documenting and easy to understand for those who didn't develop it.
Consider adding surrogate keys – columns that have no business meaning but can uniquely identify each row. Identity columns are a great example of surrogate keys.
If you do use surrogate keys be sure that their name includes the table/entity name.
For example, do not add a column called ID to each table. Instead use customer_id, supplier_id, store_id and so forth.
There are a lot of conventions and holy wars regarding the Constraints naming.
Just choose the most understandable one and follow it.
I use the following Constraint prefixes:
| Name prefix | Statement type |
|---|---|
PK_ | PRIMARY KEY |
FK_ | FOREIGN KEY |
DF_ | DEFAULT |
UQ_ | UNIQUE |
CHK_ | CHECK |
IDX_ | INDEX |
UQ_ over UK_ or UN_ which u can find in some SQL standarts, cause UQ_ isn't intercepted with others abbreviations.
The same for
CHK_ instead of CC_ or CK_ and IDX_ instead of IX_ - cause it's more precise & understandable.
Whatever, if u work in a team, just consider the team guidelines and don't mess it up with your own keys!
Tuesday, March 10, 2015
CRUD operations in SQL
CRUD acronym
CRUD is an acronym that represents four basic functions of persistent storage:| Operation | SQL |
|---|---|
| Create | INSERT |
| Read (Retrieve) | SELECT |
| Update (Modify) | UPDATE |
| Delete (Destroy) | DELETE |
These operations in SQL are described below via using MYSQL dbms:
Subscribe to:
Posts (Atom)
