Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

एसक्यूएल कमांड चीट शीट – 10 मिनट में एसक्यूएल कैसे सीखें

मैं एक एआई शोधकर्ता हूं, इसलिए मैं जिन मुख्य चीजों से निपटता हूं उनमें से एक डेटा है। एक बहुत इसका।

2.5 से अधिक एक्साबाइट डेटा के साथ हर दिन generated उत्पन्न होता है , यह कोई आश्चर्य की बात नहीं है कि इस डेटा को कहीं संग्रहीत करने की आवश्यकता है जहां हम इसकी आवश्यकता होने पर इसे एक्सेस कर सकें।

यह लेख आपको SQL के साथ जल्दी से उठने और चलाने के लिए हैक करने योग्य चीटशीट के बारे में बताएगा।

एसक्यूएल क्या है?

SQL का मतलब स्ट्रक्चर्ड क्वेरी लैंग्वेज है। यह रिलेशनल डेटाबेस मैनेजमेंट सिस्टम के लिए एक भाषा है। SQL का उपयोग आज रिलेशनल डेटाबेस में डेटा को स्टोर करने, पुनर्प्राप्त करने और हेरफेर करने के लिए किया जाता है।

यहाँ एक बुनियादी संबंधपरक डेटाबेस कैसा दिखता है:

SQL का उपयोग करके, हम क्वेरी writing लिखकर डेटाबेस के साथ इंटरैक्ट कर सकते हैं

यहाँ एक उदाहरण क्वेरी कैसी दिखती है:

SELECT * FROM customers;

इसका उपयोग करना SELECT कथन, क्वेरी सभी . का चयन करती है ग्राहक की तालिका के सभी स्तंभों का डेटा और इस प्रकार डेटा लौटाता है:

तारांकन वाइल्डकार्ड वर्ण (*) "सभी . को संदर्भित करता है ” और सभी . का चयन करता है पंक्तियों और स्तंभों। इसके बजाय हम इसे विशिष्ट कॉलम नामों से बदल सकते हैं — यहां केवल वे कॉलम क्वेरी द्वारा वापस किए जाएंगे

SELECT FirstName, LastName FROM customers;

एक WHERE जोड़ना क्लॉज आपको जो लौटाता है उसे फ़िल्टर करने की अनुमति देता है:

SELECT * FROM customers WHERE age >= 30 ORDER BY age ASC;

यह क्वेरी उत्पाद तालिका से उम्र . के साथ सभी डेटा लौटाती है 30 से अधिक का मान।

ORDER BY . का उपयोग कीवर्ड का सीधा सा मतलब है कि आयु कॉलम का उपयोग करके न्यूनतम मान से उच्चतम तक परिणामों को क्रमबद्ध किया जाएगा

INSERT INTO . का उपयोग करना कथन, हम एक तालिका में नया डेटा जोड़ सकते हैं। यहां ग्राहक तालिका में एक नया उपयोगकर्ता जोड़ने का एक बुनियादी उदाहरण दिया गया है:

INSERT INTO customers(FirstName, LastName, address, email)
VALUES ('Jason', 'Dsouza', 'McLaren Vale, South Australia', '[email protected]');

बेशक, ये उदाहरण SQL भाषा क्या कर सकते हैं, इसका केवल एक बहुत छोटा चयन प्रदर्शित करते हैं। हम इस गाइड में इसके बारे में और जानेंगे।

एसक्यूएल क्यों सीखें?

हम बिग डेटा के युग में रहते हैं, जहां डेटा का उपयोग व्यापक रूप से अंतर्दृष्टि खोजने और रणनीति, मार्केटिंग, विज्ञापन और अन्य कार्यों की अधिकता को सूचित करने के लिए किया जाता है।

Google, Amazon, AirBnb जैसे बड़े व्यवसाय ग्राहक अनुभव को बेहतर बनाने के आधार के रूप में बड़े, संबंधपरक डेटाबेस का उपयोग करते हैं। न केवल डेटा वैज्ञानिकों और विश्लेषकों के लिए बल्कि सभी के लिए SQL को समझना एक महान कौशल है।

आपको क्या लगता है कि आपको अचानक जूतों पर एक Youtube विज्ञापन मिला, जब कुछ मिनट पहले, आप अपने पसंदीदा जूतों को गुगल कर रहे थे? वह काम पर SQL (या SQL का एक रूप) है!

SQL बनाम MySQL

इससे पहले कि हम आगे बढ़ें, मैं अक्सर एक भ्रमित विषय को स्पष्ट करना चाहता हूं - SQL और MySQL के बीच का अंतर। जैसा कि यह पता चला है, वे नहीं हैं वही बात!

SQL एक भाषा है, जबकि MySQL SQL को लागू करने के लिए एक प्रणाली है।

एसक्यूएल आउटलाइन सिंटैक्स जो आपको रिलेशनल डेटाबेस को प्रबंधित करने वाली क्वेरी लिखने की अनुमति देता है।

MySQL एक डेटाबेस है सिस्टम जो सर्वर पर चलता है। यह आपको MySQL डेटाबेस को प्रबंधित करने के लिए SQL सिंटैक्स का उपयोग करके क्वेरी लिखने की अनुमति देता है।

MySQL के अलावा, अन्य सिस्टम भी हैं जो SQL को लागू करते हैं। कुछ अधिक लोकप्रिय लोगों में शामिल हैं:

  • SQLite
  • Oracle डेटाबेस
  • पोस्टग्रेएसक्यूएल
  • माइक्रोसॉफ्ट एसक्यूएल सर्वर

MySQL कैसे स्थापित करें

ज्यादातर मामलों के लिए, MySQL एक डेटाबेस प्रबंधन प्रणाली के लिए पसंदीदा विकल्प है। कई लोकप्रिय सामग्री प्रबंधन प्रणालियाँ (जैसे Wordpress) डिफ़ॉल्ट रूप से MySQL का उपयोग करती हैं, इसलिए उन अनुप्रयोगों को प्रबंधित करने के लिए MySQL का उपयोग करना एक अच्छा विचार हो सकता है।

MySQL का उपयोग करने के लिए, आपको इसे अपने सिस्टम पर इंस्टॉल करना होगा:

Windows पर MySQL इंस्टॉल करें

विंडोज़ पर MySQL को स्थापित करने का अनुशंसित तरीका MySQL वेबसाइट से MSI इंस्टॉलर का उपयोग करना है।

यह संसाधन आपको इंस्टॉलेशन प्रक्रिया में मार्गदर्शन करेगा।

macOS पर MySQL इंस्टॉल करें

MacOS पर, MySQL को स्थापित करने में एक इंस्टॉलर डाउनलोड करना भी शामिल है।

यह संसाधन संस्थापन प्रक्रिया में आपका मार्गदर्शन करेगा।

MySQL का उपयोग कैसे करें

आपके सिस्टम पर अब MySQL इंस्टाल होने के साथ, मेरा सुझाव है कि आप किसी प्रकार के SQL प्रबंधन एप्लिकेशन का उपयोग करें अपने डेटाबेस को प्रबंधित करने की प्रक्रिया को बहुत आसान बनाने के लिए।

चुनने के लिए बहुत सारे ऐप हैं जिनमें से अधिकतर एक ही काम करते हैं, इसलिए यह आपकी निजी पसंद पर निर्भर करता है कि किसका उपयोग करना है:

  • MySQL कार्यक्षेत्र Oracle द्वारा विकसित
  • phpMyAdmin (वेब ​​ब्राउज़र में काम करता है)
  • HeidiSQL (Windows के लिए अनुशंसित)
  • सीक्वल प्रो (macOS के लिए अनुशंसित)

जब आप अपनी खुद की SQL क्वेरी लिखना शुरू करने के लिए तैयार हों, तो अपना खुद का डेटाबेस बनाने के बजाय डमी डेटा आयात करने पर विचार करें।

यहां कुछ डमी डेटाबेस दिए गए हैं जो नि:शुल्क डाउनलोड के लिए उपलब्ध हैं।

एसक्यूएल चीटशीट - द आइसिंग ऑन द केक 

SQL कीवर्ड

यहाँ आप SQL कथनों में प्रयुक्त खोजशब्दों का एक संग्रह, एक विवरण, और जहाँ उपयुक्त हो एक उदाहरण पा सकते हैं। कुछ अधिक उन्नत खोजशब्दों का अपना समर्पित अनुभाग होता है।

जहां एक उदाहरण के आगे MySQL का उल्लेख किया गया है, इसका मतलब यह है कि यह उदाहरण केवल MySQL डेटाबेस पर लागू होता है (किसी भी अन्य डेटाबेस सिस्टम के विपरीत)।

ADD -- Adds a new column to an existing table

ADD CONSTRAINT -- Creates a new constraint on an existing table, which is used to specify rules for any data in the table.

ALTER TABLE -- Adds, deletes or edits columns in a table. It can also be used to add and delete constraints in a table, as per the above.

ALTER COLUMN -- Changes the data type of a table’s column.

ALL -- Returns true if all of the subquery values meet the passed condition.

AND -- Used to join separate conditions within a WHERE clause.

ANY -- Returns true if any of the subquery values meet the given condition.

AS -- Renames a table or column with an alias value which only exists for the duration of the query.

ASC -- Used with ORDER BY to return the data in ascending order.

BETWEEN -- Selects values within the given range.

CASE -- Changes query output depending on conditions.

CHECK -- Adds a constraint that limits the value which can be added to a column.

CREATE DATABASE -- Creates a new database.

CREATE TABLE -- Creates a new table. 

DEFAULT -- Sets a default value for a column

DELETE -- Delete data from a table.

DESC -- Used with ORDER BY to return the data in descending order.

DROP COLUMN -- Deletes a column from a table.

DROP DATABASE -- Deletes the entire database.

DROP DEAFULT -- Removes a default value for a column.

DROP TABLE -- Deletes a table from a database.

EXISTS -- Checks for the existence of any record within the subquery, returning true if one or more records are returned.

FROM -- Specifies which table to select or delete data from.

IN --  Used alongside a WHERE clause as a shorthand for multiple OR conditions.

INSERT INTO -- Adds new rows to a table.

IS NULL -- Tests for empty (NULL) values.

IS NOT NULL -- The reverse of NULL. Tests for values that aren’t empty / NULL.

LIKE -- Returns true if the operand value matches a pattern.

NOT -- Returns true if a record DOESN’T meet the condition.
 
OR -- Used alongside WHERE to include data when either condition is true.

ORDER BY -- Used to sort the result data in ascending (default) or descending order through the use of ASC or DESC keywords.

ROWNUM -- Returns results where the row number meets the passed condition.

SELECT -- Used to select data from a database, which is then returned in a results set.

SELECT DISTINCT -- Sames as SELECT, except duplicate values are excluded.

SELECT INTO -- Copies data from one table and inserts it into another.

SELECT TOP -- Allows you to return a set number of records to return from a table.

SET -- Used alongside UPDATE to update existing data in a table.

SOME -- Identical to ANY.

TOP -- Used alongside SELECT to return a set number of records from a table.

TRUNCATE TABLE -- Similar to DROP, but instead of deleting the table and its data, this deletes only the data.

UNION -- Combines the results from 2 or more SELECT statements and returns only distinct values.

UNION ALL -- The same as UNION, but includes duplicate values.

UNIQUE -- This constraint ensures all values in a column are unique.

UPDATE -- Updates existing data in a table.

VALUES -- Used alongside the INSERT INTO keyword to add new values to a table.

WHERE -- Filters results to only include data which meets the given condition.

एसक्यूएल में टिप्पणियाँ

टिप्पणियाँ आपको सीधे निष्पादित किए बिना, आपके SQL कथनों के अनुभागों की व्याख्या करने की अनुमति देती हैं।

SQL में 2 प्रकार की टिप्पणियाँ होती हैं, सिंगल लाइन और मल्टीलाइन।

एसक्यूएल में सिंगल लाइन कमेंट्स

सिंगल लाइन कमेंट '- -' से शुरू होते हैं। इन 2 वर्णों के बाद पंक्ति के अंत तक किसी भी पाठ को अनदेखा कर दिया जाएगा।

-- This part is ignored

SELECT * FROM customers;

SQL में मल्टीलाइन टिप्पणियाँ

बहुपंक्ति टिप्पणियाँ /* से शुरू होती हैं और */ से समाप्त होती हैं। वे कई पंक्तियों में तब तक खिंचते हैं जब तक कि समापन वर्ण नहीं मिल जाते।

/*

This is a multiline comment.
It can span across multiple lines.

*/

SELECT * FROM customers;

/*

This is another comment. 
You can even put code within a comment to prevent its execution

SELECT * FROM icecreams;

*/

MySQL में डेटा प्रकार

नई तालिका बनाते समय या किसी मौजूदा तालिका को संपादित करते समय, आपको प्रत्येक स्तंभ द्वारा स्वीकार किए जाने वाले डेटा के प्रकार को निर्दिष्ट करना होगा।

इस उदाहरण में, डेटा id . को दिया गया है कॉलम एक int (पूर्णांक) होना चाहिए, जबकि FirstName कॉलम में एक VARCHAR है अधिकतम 255 वर्णों वाला डेटा प्रकार।

CREATE TABLE customers(
id int,
FirstName varchar(255)
);

1. स्ट्रिंग डेटा प्रकार

CHAR(size) -- Fixed length string which can contain letters, numbers and special characters. The size parameter sets the maximum string length, from 0 – 255 with a default of 1.

VARCHAR(size) -- Variable length string similar to CHAR(), but with a maximum string length range from 0 to 65535.

BINARY(size) -- Similar to CHAR() but stores binary byte strings.

VARBINARY(size) -- Similar to VARCHAR() but for binary byte strings.

TINYBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 255 bytes.

TINYTEXT -- Holds a string with a maximum length of 255 characters. Use VARCHAR() instead, as it’s fetched much faster.

TEXT(size) -- Holds a string with a maximum length of 65535 bytes. Again, better to use VARCHAR().

BLOB(size) -- Holds Binary Large Objects (BLOBs) with a max length of 65535 bytes.

MEDIUMTEXT -- Holds a string with a maximum length of 16,777,215 characters.

MEDIUMBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 16,777,215 bytes.

LONGTEXT -- Holds a string with a maximum length of 4,294,967,295 characters.

LONGBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 4,294,967,295 bytes.

ENUM(a, b, c, etc…) -- A string object that only has one value, which is chosen from a list of values which you define, up to a maximum of 65535 values. If a value is added which isn’t on this list, it’s replaced with a blank value instead.

SET(a, b, c, etc…) -- A string object that can have 0 or more values, which is chosen from a list of values which you define, up to a maximum of 64 values.

2. संख्यात्मक डेटा प्रकार

BIT(size) -- A bit-value type with a default of 1. The allowed number of bits in a value is set via the size parameter, which can hold values from 1 to 64.

TINYINT(size) -- A very small integer with a signed range of -128 to 127, and an unsigned range of 0 to 255. Here, the size parameter specifies the maximum allowed display width, which is 255.

BOOL -- Essentially a quick way of setting the column to TINYINT with a size of 1. 0 is considered false, whilst 1 is considered true.

BOOLEAN	-- Same as BOOL.

SMALLINT(size) -- A small integer with a signed range of -32768 to 32767, and an unsigned range from 0 to 65535. Here, the size parameter specifies the maximum allowed display width, which is 255.

MEDIUMINT(size) -- A medium integer with a signed range of -8388608 to 8388607, and an unsigned range from 0 to 16777215. Here, the size parameter specifies the maximum allowed display width, which is 255.

INT(size) -- A medium integer with a signed range of -2147483648 to 2147483647, and an unsigned range from 0 to 4294967295. Here, the size parameter specifies the maximum allowed display width, which is 255.

INTEGER(size) -- Same as INT.

BIGINT(size) -- A medium integer with a signed range of -9223372036854775808 to 9223372036854775807, and an unsigned range from 0 to 18446744073709551615. Here, the size parameter specifies the maximum allowed display width, which is 255.

FLOAT(p) -- A floating point number value. If the precision (p) parameter is between 0 to 24, then the data type is set to FLOAT(), whilst if it's from 25 to 53, the data type is set to DOUBLE(). This behaviour is to make the storage of values more efficient.

DOUBLE(size, d) -- A floating point number value where the total digits are set by the size parameter, and the number of digits after the decimal point is set by the d parameter.

DECIMAL(size, d) -- An exact fixed point number where the total number of digits is set by the size parameters, and the total number of digits after the decimal point is set by the d parameter.

DEC(size, d) -- Same as DECIMAL.

3. दिनांक/समय डेटा प्रकार

DATE -- A simple date in YYYY-MM–DD format, with a supported range from ‘1000-01-01’ to ‘9999-12-31’.

DATETIME(fsp) -- A date time in YYYY-MM-DD hh:mm:ss format, with a supported range from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. By adding DEFAULT and ON UPDATE to the column definition, it automatically sets to the current date/time.

TIMESTAMP(fsp) -- A Unix Timestamp, which is a value relative to the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). This has a supported range from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC.
By adding DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT TIMESTAMP to the column definition, it automatically sets to current date/time.

TIME(fsp) -- A time in hh:mm:ss format, with a supported range from ‘-838:59:59’ to ‘838:59:59’.

YEAR -- A year, with a supported range of ‘1901’ to ‘2155’.

SQL ऑपरेटर्स

1. SQL में अंकगणित संचालिका

+ -- Add
– -- Subtract
* -- Multiply
/ -- Divide
% -- Modulus

2. SQL में बिटवाइज़ ऑपरेटर्स

& -- Bitwise AND
| -- Bitwise OR
^-- Bitwise XOR

3. SQL में तुलना ऑपरेटर्स

= -- Equal to
> -- Greater than
< -- Less than
>= -- Greater than or equal to
<= -- Less than or equal to
<> -- Not equal to

4. SQL में कंपाउंड ऑपरेटर्स

+= -- Add equals
-= -- Subtract equals
*= -- Multiply equals
/= -- Divide equals
%= -- Modulo equals
&= -- Bitwise AND equals
^-= -- Bitwise exclusive equals
|*= -- Bitwise OR equals

एसक्यूएल फंक्शंस

1. SQL में स्ट्रिंग फ़ंक्शंस

ASCII -- Returns the equivalent ASCII value for a specific character.

CHAR_LENGTH -- Returns the character length of a string.

CHARACTER_LENGTH -- Same as CHAR_LENGTH.

CONCAT -- Adds expressions together, with a minimum of 2.

CONCAT_WS -- Adds expressions together, but with a separator between each value.

FIELD -- Returns an index value relative to the position of a value within a list of values.

FIND IN SET -- Returns the position of a string in a list of strings.

FORMAT -- When passed a number, returns that number formatted to include commas (eg 3,400,000).

INSERT -- Allows you to insert one string into another at a certain point, for a certain number of characters.

INSTR -- Returns the position of the first time one string appears within another.

LCASE -- Converts a string to lowercase.

LEFT -- Starting from the left, extracts the given number of characters from a string and returns them as another.

LENGTH -- Returns the length of a string, but in bytes.

LOCATE -- Returns the first occurrence of one string within another,

LOWER -- Same as LCASE.

LPAD -- Left pads one string with another, to a specific length.

LTRIM -- Removes any leading spaces from the given string.

MID -- Extracts one string from another, starting from any position.

POSITION -- Returns the position of the first time one substring appears within another.

REPEAT -- Allows you to repeat a string

REPLACE -- Allows you to replace any instances of a substring within a string, with a new substring.

REVERSE	-- Reverses the string.

RIGHT -- Starting from the right, extracts the given number of characters from a string and returns them as another.

RPAD -- Right pads one string with another, to a specific length.

RTRIM -- Removes any trailing spaces from the given string.

SPACE -- Returns a string full of spaces equal to the amount you pass it.

STRCMP -- Compares 2 strings for differences

SUBSTR -- Extracts one substring from another, starting from any position.

SUBSTRING -- Same as SUBSTR

SUBSTRING_INDEX	-- Returns a substring from a string before the passed substring is found the number of times equals to the passed number.

TRIM --	Removes trailing and leading spaces from the given string. Same as if you were to run LTRIM and RTRIM together.

UCASE -- Converts a string to uppercase.

UPPER -- Same as UCASE.

2. SQL में संख्यात्मक कार्य

ABS -- Returns the absolute value of the given number.

ACOS -- Returns the arc cosine of the given number.

ASIN -- Returns the arc sine of the given number.

ATAN -- Returns the arc tangent of one or 2 given numbers.

ATAN2 -- Returns the arc tangent of 2 given numbers.

AVG -- Returns the average value of the given expression.

CEIL -- Returns the closest whole number (integer) upwards from a given decimal point number.

CEILING -- Same as CEIL.

COS -- Returns the cosine of a given number.

COT -- Returns the cotangent of a given number.

COUNT -- Returns the amount of records that are returned by a SELECT query.

DEGREES -- Converts a radians value to degrees.

DIV -- Allows you to divide integers.

EXP -- Returns e to the power of the given number.

FLOOR -- Returns the closest whole number (integer) downwards from a given decimal point number.

GREATEST -- Returns the highest value in a list of arguments.

LEAST -- Returns the smallest value in a list of arguments.

LN -- Returns the natural logarithm of the given number.

LOG -- Returns the natural logarithm of the given number, or the logarithm of the given number to the given base.

LOG10 -- Does the same as LOG, but to base 10.

LOG2 -- Does the same as LOG, but to base 2.

MAX -- Returns the highest value from a set of values.

MIN -- Returns the lowest value from a set of values.

MOD -- Returns the remainder of the given number divided by the other given number.

PI -- Returns PI.

POW -- Returns the value of the given number raised to the power of the other given number.

POWER -- Same as POW.

RADIANS -- Converts a degrees value to radians.

RAND -- Returns a random number.

ROUND -- Rounds the given number to the given amount of decimal places.

SIGN -- Returns the sign of the given number.

SIN -- Returns the sine of the given number.

SQRT -- Returns the square root of the given number.

SUM -- Returns the value of the given set of values combined.

TAN -- Returns the tangent of the given number.

TRUNCATE -- Returns a number truncated to the given number of decimal places.

3. SQL में दिनांक कार्य

ADDDATE -- Adds a date interval (eg: 10 DAY) to a date (eg: 20/01/20) and returns the result (eg: 20/01/30).

ADDTIME -- Adds a time interval (eg: 02:00) to a time or datetime (05:00) and returns the result (07:00).

CURDATE -- Gets the current date.

CURRENT_DATE -- Same as CURDATE.

CURRENT_TIME -- Gest the current time.

CURRENT_TIMESTAMP -- Gets the current date and time.

CURTIME -- Same as CURRENT_TIME.

DATE -- Extracts the date from a datetime expression.

DATEDIFF -- Returns the number of days between the 2 given dates.

DATE_ADD -- Same as ADDDATE.

DATE_FORMAT -- Formats the date to the given pattern.

DATE_SUB -- Subtracts a date interval (eg: 10 DAY) to a date (eg: 20/01/20) and returns the result (eg: 20/01/10).

DAY -- Returns the day for the given date.

DAYNAME -- Returns the weekday name for the given date.

DAYOFWEEK -- Returns the index for the weekday for the given date.

DAYOFYEAR -- Returns the day of the year for the given date.

EXTRACT -- Extracts from the date the given part (eg MONTH for 20/01/20 = 01).

FROM DAYS -- Returns the date from the given numeric date value.

HOUR -- Returns the hour from the given date.

LAST DAY -- Gets the last day of the month for the given date.

LOCALTIME -- Gets the current local date and time.

LOCALTIMESTAMP -- Same as LOCALTIME.

MAKEDATE -- Creates a date and returns it, based on the given year and number of days values.

MAKETIME -- Creates a time and returns it, based on the given hour, minute and second values.

MICROSECOND -- Returns the microsecond of a given time or datetime.

MINUTE -- Returns the minute of the given time or datetime.

MONTH -- Returns the month of the given date.

MONTHNAME -- Returns the name of the month of the given date.

NOW -- Same as LOCALTIME.

PERIOD_ADD -- Adds the given number of months to the given period.

PERIOD_DIFF -- Returns the difference between 2 given periods.

QUARTER -- Returns the year quarter for the given date.

SECOND -- Returns the second of a given time or datetime.

SEC_TO_TIME -- Returns a time based on the given seconds.

STR_TO_DATE -- Creates a date and returns it based on the given string and format.

SUBDATE -- Same as DATE_SUB.

SUBTIME -- Subtracts a time interval (eg: 02:00) to a time or datetime (05:00) and returns the result (03:00).

SYSDATE -- Same as LOCALTIME.

TIME -- Returns the time from a given time or datetime.

TIME_FORMAT -- Returns the given time in the given format.

TIME_TO_SEC -- Converts and returns a time into seconds.

TIMEDIFF -- Returns the difference between 2 given time/datetime expressions.

TIMESTAMP -- Returns the datetime value of the given date or datetime.

TO_DAYS -- Returns the total number of days that have passed from ‘00-00-0000’ to the given date.

WEEK -- Returns the week number for the given date.

WEEKDAY -- Returns the weekday number for the given date.

WEEKOFYEAR -- Returns the week number for the given date.

YEAR -- Returns the year from the given date.

YEARWEEK -- Returns the year and week number for the given date.

4. SQL में विविध कार्य

BIN -- Returns the given number in binary.

BINARY -- Returns the given value as a binary string.

CAST -- Converst one type into another.

COALESCE -- From a list of values, returns the first non-null value.

CONNECTION_ID -- For the current connection, returns the unique connection ID.

CONV -- Converts the given number from one numeric base system into another.

CONVERT -- Converts the given value into the given datatype or character set.

CURRENT_USER -- Returns the user and hostname which was used to authenticate with the server.

DATABASE -- Gets the name of the current database.

GROUP BY -- Used alongside aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the results.

HAVING -- Used in the place of WHERE with aggregate functions.

IF -- If the condition is true it returns a value, otherwise it returns another value.

IFNULL -- If the given expression equates to null, it returns the given value.

ISNULL -- If the expression is null, it returns 1, otherwise returns 0.

LAST_INSERT_ID -- For the last row which was added or updated in a table, returns the auto increment ID.

NULLIF -- Compares the 2 given expressions. If they are equal, NULL is returned, otherwise the first expression is returned.

SESSION_USER -- Returns the current user and hostnames.

SYSTEM_USER -- Same as SESSION_USER.

USER -- Same as SESSION_USER.

VERSION -- Returns the current version of the MySQL powering the database.

SQL में वाइल्डकार्ड वर्ण

SQL में, वाइल्डकार्ड विशेष वर्ण होते हैं जिनका उपयोग LIKE . के साथ किया जाता है और NOT LIKE खोजशब्द। यह हमें परिष्कृत पैटर्न के साथ डेटा को कुशलता से खोजने की अनुमति देता है।

% -- Equates to zero or more characters.
-- Example: Find all customers with surnames ending in ‘ory’.
SELECT * FROM customers
WHERE surname LIKE '%ory';

_ -- Equates to any single character.
-- Example: Find all customers living in cities beginning with any 3 characters, followed by ‘vale’.
SELECT * FROM customers
WHERE city LIKE '_ _ _vale';

[charlist] -- Equates to any single character in the list.
-- Example: Find all customers with first names beginning with J, K or T.
SELECT * FROM customers
WHERE first_name LIKE '[jkt]%';

SQL कुंजियाँ

संबंधपरक डेटाबेस में, प्राथमिक . की अवधारणा है और विदेशी चांबियाँ। SQL तालिकाओं में, इन्हें बाधाओं के रूप में शामिल किया जाता है, जहाँ एक तालिका में प्राथमिक कुंजी, एक विदेशी कुंजी या दोनों हो सकते हैं।

1. SQL में प्राथमिक कुंजियाँ

एक प्राथमिक तालिका में प्रत्येक रिकॉर्ड को विशिष्ट रूप से पहचानने देता है। आपके पास प्रति तालिका केवल एक प्राथमिक कुंजी हो सकती है, और आप इस बाधा को किसी एकल या स्तंभों के संयोजन के लिए निर्दिष्ट कर सकते हैं। हालांकि, इसका मतलब है कि इस कॉलम में प्रत्येक मान अद्वितीय होना चाहिए।

आम तौर पर किसी तालिका में, आईडी कॉलम प्राथमिक कुंजी होता है, और आमतौर पर AUTO_INCREMENT के साथ जोड़ा जाता है खोजशब्द। इसका मतलब है कि नए रिकॉर्ड बनने पर मान अपने आप बढ़ जाता है।

उदाहरण (MySQL)

एक नई तालिका बनाएं और प्राथमिक कुंजी को आईडी कॉलम पर सेट करें।

CREATE TABLE customers (
id int NOT NULL AUTO_INCREMENT,
FirstName varchar(255),
Last Name varchar(255) NOT NULL,
address varchar(255),
email varchar(255),
PRIMARY KEY (id)
);

2. SQL में विदेशी कुंजियाँ

आप एक कॉलम या कई के लिए एक विदेशी कुंजी लागू कर सकते हैं। आप इसका उपयोग लिंक . करने के लिए करते हैं एक रिलेशनल डेटाबेस में एक साथ 2 टेबल।

विदेशी कुंजी वाली तालिका को बच्चा . कहा जाता है कुंजी,

संदर्भित (या उम्मीदवार) कुंजी वाली तालिका को पैरेंट . कहा जाता है टेबल।

इसका अनिवार्य रूप से मतलब है कि कॉलम डेटा 2 तालिकाओं के बीच साझा किया जाता है, क्योंकि एक विदेशी कुंजी भी अमान्य डेटा को सम्मिलित करने से रोकती है जो कि मूल तालिका में भी मौजूद नहीं है।

उदाहरण (MySQL)

एक नई तालिका बनाएं और अन्य तालिकाओं में आईडी को संदर्भित करने वाले किसी भी स्तंभ को विदेशी कुंजियों में बदलें।

CREATE TABLE orders (
id int NOT NULL,
user_id int,
product_id int,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);

एसक्यूएल में इंडेक्स

अनुक्रमणिका ऐसी विशेषताएँ हैं जिन्हें डेटा पुनर्प्राप्ति को एक तेज़ और अधिक कुशल प्रक्रिया बनाने के लिए अक्सर खोजे जाने वाले स्तंभों को असाइन किया जा सकता है।

CREATE INDEX -- Creates an index named ‘idx_test’ on the first_name and surname columns of the users table. In this instance, duplicate values are allowed.
CREATE INDEX idx_test
ON users (first_name, surname);
CREATE UNIQUE INDEX -- The same as the above, but no duplicate values.
CREATE UNIQUE INDEX idx_test
ON users (first_name, surname);
DROP INDEX -- Removes an index.
ALTER TABLE users
DROP INDEX idx_test;

SQL जॉइन करता है

SQL में, एक JOIN क्लॉज का उपयोग एक ऐसे परिणाम को वापस करने के लिए किया जाता है जो एक सामान्य कॉलम के आधार पर कई तालिकाओं के डेटा को जोड़ता है, जो उन दोनों में प्रदर्शित होता है।

आपके उपयोग के लिए कई अलग-अलग जॉइन उपलब्ध हैं:

  • आंतरिक जुड़ाव (डिफ़ॉल्ट): दोनों तालिकाओं में मेल खाने वाले मान वाले किसी भी रिकॉर्ड को लौटाता है।
  • बाएं शामिल हों: दूसरी तालिका के किसी भी मिलान रिकॉर्ड के साथ, पहली तालिका के सभी रिकॉर्ड लौटाता है।
  • राइट जॉइन करें: पहली तालिका के किसी भी मिलान रिकॉर्ड के साथ, दूसरी तालिका के सभी रिकॉर्ड लौटाता है।
  • पूर्ण रूप से शामिल हों: मैच होने पर दोनों तालिकाओं के सभी रिकॉर्ड लौटाता है।

यह देखने का एक सामान्य तरीका है कि कैसे जुड़ता है कार्य इस प्रकार है:

SELECT orders.id, users.FirstName, users.Surname, products.name as ‘product name’
FROM orders
INNER JOIN users on orders.user_id = users.id
INNER JOIN products on orders.product_id = products.id;

एसक्यूएल में देखे जाने की संख्या

एक दृश्य अनिवार्य रूप से एक SQL परिणाम सेट है जो एक लेबल के तहत डेटाबेस में संग्रहीत हो जाता है, ताकि आप बाद में क्वेरी को फिर से चलाए बिना उस पर वापस आ सकें।

ये विशेष रूप से तब उपयोगी होते हैं जब आपके पास एक महंगी SQL क्वेरी होती है जिसकी आपको कई बार आवश्यकता हो सकती है। इसलिए समान परिणाम सेट उत्पन्न करने के लिए इसे बार-बार चलाने के बजाय, आप इसे केवल एक बार कर सकते हैं और इसे एक दृश्य के रूप में सहेज सकते हैं।

एसक्यूएल में दृश्य कैसे बनाएं

एक दृश्य बनाने के लिए, आप ऐसा इस तरह कर सकते हैं:

CREATE VIEW priority_users AS
SELECT * FROM users
WHERE country = ‘United Kingdom’;

फिर भविष्य में, यदि आपको संग्रहीत परिणाम सेट तक पहुंचने की आवश्यकता है, तो आप ऐसा इस प्रकार कर सकते हैं:

SELECT * FROM [priority_users];

एसक्यूएल में दृश्य कैसे बदलें

CREATE OR REPLACE के साथ कमांड, आप इस तरह के दृश्य को अपडेट कर सकते हैं:

CREATE OR REPLACE VIEW [priority_users] AS
SELECT * FROM users
WHERE country = ‘United Kingdom’ OR country=’USA’;

एसक्यूएल में दृश्य कैसे हटाएं

किसी दृश्य को हटाने के लिए, बस DROP VIEW . का उपयोग करें आदेश।

DROP VIEW priority_users;

निष्कर्ष

अधिकांश वेबसाइट और एप्लिकेशन किसी न किसी तरह से रिलेशनल डेटाबेस का उपयोग करते हैं। यह SQL को जानने के लिए अत्यंत मूल्यवान बनाता है क्योंकि यह आपको अधिक जटिल, कार्यात्मक सिस्टम बनाने की अनुमति देता है।

भविष्य के लेखों के अपडेट के लिए मुझे ट्विटर पर फॉलो करना न भूलें। हैप्पी लर्निंग!



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Myd, .myi, .frm फ़ाइलों से MySQL डेटाबेस को कैसे पुनर्प्राप्त करें

  2. थ्रेड मुख्य java.sql.SQLException में अपवाद:उपयोगकर्ता ''@'लोकलहोस्ट' के लिए प्रवेश निषेध (पासवर्ड का उपयोग करके:नहीं)

  3. SQL में ROW_NUMBER - SQL और SQL सर्वर में शीर्ष उदाहरण चुनें

  4. कमांड लाइन का उपयोग करके डेटाबेस कैसे आयात करें

  5. एकल MySQL क्वेरी में विभिन्न स्थितियों के साथ कई मायने रखता है