Found 6702 Articles for Database

Discuss the history of MySQL

AmitDiwan
Updated on 24-Feb-2021 11:19:38

2K+ Views

MySQL is an open source SQL (structured query language) database management system.  It is a system that helps store and manage data efficiently. Database generally stores data in a structured fashion.Timeline of MySQLUnireg, which is the code base of MySQL, was started in 1981.MySQL was founded in 1995 in Sweden.In 2000, MySQL went open source, so it could be accessed and used by all.In the year 2001, Marten Mickos was elected as the CEO of MySQL.In the year 2002, MySQL launched its headquarters in USA, in addition to Sweden headquarters.In 2003, MySQL entered into a partnership with SAP, and many features ... Read More

Discuss few characteristics of MySQL

AmitDiwan
Updated on 24-Feb-2021 11:09:55

534 Views

MySQL is an open source SQL (structured query language) database management system. Let us see some of its characteristics:ConsistentMySQL server is quick, and reliable. It stores data efficiently in the memory ensuring that data is consistent, and not redundant.ScalableMySQL server is scalable and easy to use. Scalability refers to the ability of systems to work easily with small amounts of data, large amounts of data, clusters of machines, and so on.  It is also used in production environment due to its scalability and ease of use.Databases over InternetIt provides high security, improved connectivity, and speed thereby making it suitable to ... Read More

Create Primary Key on an existing table in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 13:09:44

2K+ Views

Although quite infrequent, you may come across situations wherein you need to define the primary key on an existing table. This can be achieved using the ALTER TABLE statement.The syntax is −ALTER TABLE table_name ADD PRIMARY KEY (column_name1, column_name2, …., columns_nameN)As can be seen from the above syntax, you can define PRIMARY KEY on multiple columns. When you have defined the PRIMARY KEY on multiple columns, the condition is that the column pairs should have unique and non-null values. Thus, if the PRIMARY KEY is defined on (column1, column2), the values (value1, value2), (value3, value2), and (value1, value4) are allowed. ... Read More

Extract day, hour, minute, etc. from a datetime column in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 13:08:17

1K+ Views

Let us create a new table containing a single timestamp column −CREATE TABLE timestamp_test(    ts timestamp );Now let us populate it with some data −INSERT INTO timestamp_test(ts) VALUES(current_timestamp), (current_timestamp+interval '5 days'), (current_timestamp-interval '18 hours'), (current_timestamp+interval '1 year'), (current_timestamp+interval '3 minutes'), (current_timestamp-interval '6 years');If you query the table (SELECT * from timestamp_test), you will see the following output −ts2021-01-30 19:23:24.0080872021-02-04 19:23:24.0080872021-01-30 01:23:24.0080872022-01-30 19:23:24.0080872021-01-30 19:26:24.0080872015-01-30 19:23:24.008087Now, in order to extract hour, minute, etc. from the timestamp column, we use the EXTRACT function. Some examples are shown below −SELECT EXTRACT(HOUR from ts) as hour from timestamp_testOutput −hour19191191919Similarly −SELECT EXTRACT(MONTH from ts) as ... Read More

Aliasing in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 13:05:41

176 Views

Often, we have some very long table names, and writing the table name every time is troublesome. We can use aliasing to help us there, thanks to which, we will need to write the long table name only once.The table aliases are generally written in the FROM part of the statement, or the JOIN part.For example, consider that we have two tables, marks, and student_info, defined respectively below −marksnameroll_noperc_marksAniket1224Siddhi4565Yash2642Isha5687student_infonameroll_noagegenderAniket1226MIsha5625FSiddhi4523FYash2625MNow, if you want to see the name, roll_no, perc_marks, and age of the student in one query, your query will look like this −SELECT marks.name, marks.roll_no, marks.perc_marks, student_info.age FROM marks LEFT ... Read More

How to combine different columns of a table to yield a single column in query output in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 13:02:48

1K+ Views

Suppose you have a table user_info that contains the state and district of different users. An example is given below −namedistrictstateAnilMumbaiMaharashtraJoyJhalawarRajasthanRonPuneMaharashtraReenaMeerutUttar PradeshNow, if you want to combine the state and district in a single field called location, this is how you should be able to do it −SELECT name, district || ', ' || state as location from user_infoThe || operator is the string concatenation operator. The output will be −namelocationAnilMumbai, MaharashtraJoyJhalawar, RajasthanRonPune, MaharashtraReenaMeerut, Uttar PradeshSimilar operations can also be performed on numerical values. Suppose you have a table marks containing the total marks scored by students and the maximum ... Read More

How to look for partial string matches in queries in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 13:00:52

4K+ Views

Suppose you have a table user_info containing the names of users and their addresses. An example is given below −nameaddressAnilAndheri, Mumbai, MaharashtraJoyChandni Chowk, DelhiRonBandra, Mumbai, MaharashtraReenaOld Airport Road, Bengaluru, KarnatakaNow, if you want to just extract the information of users who stay in Mumbai, you can do that using the LIKE command and the % operator.SELECT * from user_info where address LIKE '%Mumbai%'The output will benameaddressAnilAndheri, Mumbai, MaharashtraRonBandra, Mumbai, MaharashtraNotice that we have added % operator on both sides of Mumbai. This means that anything can precede Mumbai and anything can be after Mumbai. We just want the string to ... Read More

How to apply DISTINCT constraint on select columns in queries in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 12:57:26

121 Views

Suppose you have a table exam_scores containing 5 columns. An example is given below with some dummy data.nameroll_nosubjecttotal_marksmarks_obtainedAnil1English10056Anil1Math10065Anil1Science10045Roy2English10078Roy2Math10098Roy2Science10067Now, one student could have sat for exams of multiple subjects, and therefore, there are multiple rows for 1 student. If you wish to find out the total number of students in the class, you may want to find the number of distinct values of roll_no. You can apply the distinct constraint on a specific column as follows −SELECT DISTINCT ON (roll_no) name, roll_no FROM exam_scores ORDER BY roll_no DESCHere’s what the output of the above query will look like −nameroll_noRoy2Anil1You can also ... Read More

CASE WHEN in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 12:55:49

2K+ Views

If you are a programmer, you may be very familiar with IF-ELSE statements. The equivalent in PostgreSQL is CASE WHEN.Let’s understand with an example. If you have table marks containing percentage marks of a student, and you want to find out whether the students have passed or failed. An example table is given below.nameperc_marksAnil24Joy65Ron42Reena87Say the passing marks are 40. Now, if the student has scored above 40 marks, we want to print ‘PASS’ against that student’s name, otherwise ‘FAIL’. This is how you can do it −SELECT name, CASE WHEN perc_marks >= 40 THEN 'PASS' ELSE 'FAIL' END status from ... Read More

How to get current timestamp and relative timestamps in PostgreSQL?

Yash Sanghvi
Updated on 02-Feb-2021 12:54:56

2K+ Views

Quite often, you need the current timestamp in PostgreSQL. You do that as follows −SELECT current_timestampIt will output the current time. The output will look like the following −2021-01-30 15:52:14.738867+00Now, what if you want the relative time instead of the current time? For example, if you want the time corresponding to 5 hours prior to the current time, you can get it using intervals.SELECT current_timestamp - interval '5 hours'The output will be different every time. At the time of writing this, the output was −2021-01-30 10:57:13.28955+00 You can also do these operations on date instead of timestampsSELECT current_dateOutput2021-01-30SELECT current_date + ... Read More

Advertisements