sql – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Wed, 17 Aug 2022 07:21:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg sql – Programmerbay https://programmerbay.com 32 32 Difference Between Primary Key and Secondary Key https://programmerbay.com/difference-between-primary-key-and-secondary-key/ https://programmerbay.com/difference-between-primary-key-and-secondary-key/#respond Tue, 19 Jul 2022 06:25:00 +0000 https://www.programmerbay.com/difference-between-primary-key-and-secondary-key/

PRIMARY KEY

A primary key is a field that identifies each record in a database table admitting that the primary key must contain its UNIQUE values.

The primary key column cannot have NULL values.

A table can have only one primary key constraint which may consist of single and multiple fields.
When multiple keys are used in a single primary key, these fields are called a composite key.

When we specify the primary key constraint for a table, the database engine executes the data uniqueness by automatically creating an index. this index permits fast access to data when the primary key is used.

Primary Key can be specified either when the table is created using CREATE table or by changing the existing table structure using ALTER.

CREATE TABLE Customer
(SID integer,
Last_Name Varchar(20)
First_Name Varchar(20)
PRIMARY KEY (SID)
ALTER TABLE Customer ADD PRIMARY KEY (SID)

SECONDARY KEY

A secondary key shows the secondary value that is unique for each record. It can be used to identify the record and it is usually indexed. It is also termed as Alternate key.

A table may have a primary key that is system generated and a secondary key that comes from the sources and other processes. that is the secondary key. it is made on a field to be indexed for faster searches. A table can have more than one secondary key.

Difference between Primary key and Secondary key ?

The main difference between primary key and secondary key is, a key that is selected for identifying each tuple in a table uniquely is termed as primary key, whereas,  a key that is not selected for identifying rows, even though it is capable of determining tuples uniquely in the table are termed as the secondary key.

BasisPrimary keySecondary key / Alternate key
DefinitionA key that is selected for identifying each record in a database table uniquelyA key that is not selected as primary key but capable of identifying each records uniquely
NULL ValuesNot AllowedAllowed
Number of KeysA table can have only one primary keyA table can have any number of secondary or alternate keys

 

For Example ,

Primary vs Secondary key

In the above table, we can identify candidate keys which are :

user_id, account_number and email 

Now from them, we can select only one primary key, in this case, we selected user_id as the primary key. Other remaining keys such as account_number and email as Secondary or Alternate key.

Summary :

A Candidate Key can be defined as an individual column or combination of columns that are used to uniquely identify rows in a table. Both the Primary Key and Secondary key are Candidate Keys. They are capable of determining each and every tuple.

Difference between primary key and secondary key

Frequently Asked Questions:

What is an alternate key ?

An alternate key can be defined as the candidate key that satisfies all the requirements to become a primary key. It’s also termed as secondary key

Is alternate key same as secondary key?

Yes, both are used interchangeably.

 

]]>
https://programmerbay.com/difference-between-primary-key-and-secondary-key/feed/ 0
Difference Between In and Between Operator In SQL https://programmerbay.com/sql-between-and-in-keywords/ https://programmerbay.com/sql-between-and-in-keywords/#respond Wed, 04 May 2022 05:47:00 +0000 http://programmerbay.com/?p=3103

In this article, we’ll be discussing the comparison between IN and BETWEEN Operator in SQL.

However, the main difference is, BETWEEN Operator fetches tuples that lies within the specified range, whereas, IN Operator is used as an alternative to multiple ORs.

Difference between In and Between Operator in SQL Tabular form

BasisBetween OperatorIN Operator
DefinitionIt is used to get a set of records ranging between two valuesIt is used to get records whose column's value matches with given set of values
SyntaxSELECT * FROM tableName WHERE Column BETWEEN range1 AND range2;SELECT * FROM tableName WHERE name IN(value,value1,value2.....);
WorkingReturn the records whose column's value lies in between the given rangeCompares the given column's value, returns that record if a match exists in the set of values

Between Operator

This Keyword is used to get a set of records which falls within a given range. In other words, It is equivalent to <= lower_bound and >=upper_bound.

It helps to  shrink a long statement (1<=id AND id<=5) to  Id BETWEEN 1 AND 5. We can use it with numeric values as well as alphabates.

Syntax:
SELECT * FROM tableName WHERE Column BETWEEN range1 AND range2;

For Example, there is a table coder and the table looks like:

coder table

Suppose, We require all the records whose ID ranges from 1 to 3.

SELECT * FROM coder WHERE id BETWEEN 1 AND 3;

between example

IN Operator

Instead of using, multiple ORs, we have IN keyword to select only those records whose column’s value matches with given values.

Like BETWEEN keyword, It also reduces the length of statements.

Syntax:

SELECT * FROM tableName WHERE name IN(value,value1,value2…..);

Note: if it isn’t numeric then each value must be enclosed within quotes.

It would return only those records whose targeted column consists values that matches with mentioned  set of values.

For Example:
This the table from which I wish to select records of  ‘john’ and ‘micky’ from coder table

coder table

SELECT * FROM coder WHERE first_name IN (‘john’,’micky’);

in output

]]>
https://programmerbay.com/sql-between-and-in-keywords/feed/ 0
Explain Limit Keyword and ORDER BY Keyword in SQL https://programmerbay.com/explain-limit-keyword-and-order-by-keyword-in-sql/ https://programmerbay.com/explain-limit-keyword-and-order-by-keyword-in-sql/#respond Mon, 17 Aug 2020 16:06:00 +0000 https://www.programmerbay.com/limit-keyword-in-sql-for-definning-the-range/

SQL LIMIT Keyword

LIMIT keyword in SQL is often used for defining the range of data to be selected and could also specify the number of rows you want to get from the table as well as from the DBMS.

Mainly, it’s used for optimization. it helps you to select a particular range of rows from the table rather than the entire row.

Syntax: SELECT * FROM table_name LIMIT Required_rows;

Usage of LIMIT Keyword:

1.  Defining a limit with a single value would keep fetching records from the table until it reached to a specified number of rows.

For example: Let say you need first 3 records from the table. To get the result the query would be.

SELECT * FROM coder LIMIT 3;

Limit keyword usage

Explanation:- In above example, first three rows had been selected from the table as it is mention to LIMIT keyword.

2. Specifying limit with two values that is Connected by a comma. The first value specifies the initial row or point (use the index which starts from O, not from 1) and other value mentions the number of record required.

For example: Suppose there is table called user details and lets say you need data starting from 3rd raw and consecutively needed  2 records from database.

Actual Table:

coder table

SELECT * FROM coder LIMIT 3,2;

limit usage 2

Explanation: In above example, the limit had started from 4 as initial point specified 3 (that is from 4th row) and kept going till the number of rows reached to 2.

ORDER BY Keyword to sort data in SQL for Sorting table

In SQL, One can retrieve columns or fields in increasing or decreasing order from a table. for this ORDER BY keyword is used to sort a record in a particular order.

 

You can sort  more than one column at a time . If the field is numeric then it would sort in numerical order (1,2,3..) and if the field is not numeric then it would sort the field in Alphabetical order(A,B,C,D).

Syntax:
SELECT column1,column2…. FROM Table_Name ORDER BY column1,column2… ASC/DESC;

Explanation:
Above syntax describes, SELECT one column or more than one column FROM table named xyz in order of increasing order(ASC) or decreasing order(DESC) as per the column.

– To retrieve a record in increasing or ascending order. Suppose you want to fetch a field named customer_name in increasing order from a table named customer. this will be  the syntax

SELECT * FROM `customer` ORDER BY customer_name ASC;

asc

SELECT customer_name FROM `customer` ORDER BY customer_name ASC;

desc1

– To retrieve a column from table in decreasing or descending order. Now Suppose you need to fetch a field named customer_name in decreasing order from a table named customer.  this is the syntax

SELECT * FROM `customer` ORDER BY customer_name DESC;

desc

SELECT customer_name FROM `customer` ORDER BY customer_name DESC;

asc1

]]>
https://programmerbay.com/explain-limit-keyword-and-order-by-keyword-in-sql/feed/ 0
How to Download And Install Xampp On Windows 10 ? https://programmerbay.com/how-to-install-xampp-for-windows-8-or-onwards/ https://programmerbay.com/how-to-install-xampp-for-windows-8-or-onwards/#respond Tue, 14 Jul 2020 16:23:00 +0000 https://www.programmerbay.com/how-to-install-xampp/

Xampp is a open source web server which comes up with handy  tools like Apache,Tomcat,Filezilla along with  Mysql. These are the following steps to install the Xampp onto your computer or laptop.

It offers you handy tools which provide a developer or beginner an environment to develop and test an application on its Windows system.

In other words, it simply provides a local server environment for developers. It comes with MariaDB, Apache Server for different languages such as Perl and PHP.

Xampp stands for X (cross) cross-platform,Apache server, MariaDB, PHP and Perl

Since, it consists of Mysql, so instead of using command prompt for practising of SQL language its better to use more advance and user friendly tool like Xampp.

Steps to Download And Install Xampp On Windows 10 :

1. First you need to download Xampp from its website ( https://www.apachefriends.org/download.html ) or simply click here and install it. It’s available for both Windows ( except Window XP) as well as OS. You will see the following  page.

2. After downloading, run the downloaded file, you will come across the Xampp Setup Wizard where you need to click on “Next”.

 

  • Immediately after which you’ll see the “Select Component” modal. On this page, you’ll find various options in form of checkboxes to choose from. However, critical components that are required to run the Xampp would automatically get installed. Now click on Next.
  • Later, you’ll see an installation folder page where you have to select a location to install the software.
  • Again click on next, Bitnami for Xampp page would be visible, if you are interested in learning more about Bitnami check the checkbox, otherwise, go to the next page.
  • Lastly, you will find a ‘ready to Install’ page, clicking on ‘Next’ will start the software installation

2. After installation, open the Xampp folder which resides within the selected drive. Afterwards click and run the executable file named Xampp control.

3. After that  you see the control panel and start the MySQL and Apache service.

4. Now, go to your browser and type  ‘localhost’ in your address bar.

 

5. You would see the Xampp dashboard and click on ‘phpmyadmin’ that would appear on navigation  bar.

6. Now, you are in MySQL where you could work out with queries.

]]>
https://programmerbay.com/how-to-install-xampp-for-windows-8-or-onwards/feed/ 0
How to Select Distinct Rows in SQL ? https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/ https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/#respond Fri, 16 Aug 2019 17:09:00 +0000 https://www.programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/

The SELECT DISTINCT statement would let you get only unique information from a field that consists lots of duplicate data. In other words, a field that contains  lots of repeated values, to ignore those repeated values the SELECT DISTINCT statement is used.

It won’t allow the column’s data to repeat more than a single time and it gives filtered information with optimization as it doesn’t fetch entire duplicate information (take less memory).

Syntax:

SELECT DISTINCT feild1,feild2,.. FROM  Table_name;

For Example:

Suppose you have a table namely as user_details:

distinct question


and if you want to fetch only non repeated value of gender field, the command would be:

SELECT DISTINCT gender FROM user_details; 

Output:

distinct

 

]]>
https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/feed/ 0
Connect to SQL from command prompt in Windows https://programmerbay.com/steps-to-work-with-sql-using-command-prompt/ https://programmerbay.com/steps-to-work-with-sql-using-command-prompt/#respond Mon, 22 Jul 2019 16:06:00 +0000 https://www.programmerbay.com/steps-to-work-with-sql-using-command-prompt/

After installing the Xampp for Windows, you  can use command prompt for practicing SQL on your desktop.
These are the following steps for using command prompt in order to do SQL practice.

1. Firstly, go to Start tab from your desktop and type in command prompt in the search box.

 

command%2Bprompt1

 

2. In command prompt, type in following path as mentioned below.
( Note: Xampp  should be installed and services must be started specially Mysql from its control panel ‘go there’).
  
C:\Users\Sandeep>cd/     /* ‘cd/’ is used to go to the root directory */
C:\>cd xampp             /* ‘cd’ is used to change the path */
C:\xampp>cd mysql        
C:\xampp\mysql>cd bin    / These commands simply represent ‘C:xamppmysqlbin’ */

command%2Bprompt


3. Now, you need to specify your MySQL username  which is usually root  and there password don’t required .

C:\xmapp\mysql\bin>mysql -u root -p

command%2Bpromp2t


4. Now you are in MySQL.

command%2Bprompt3





]]>
https://programmerbay.com/steps-to-work-with-sql-using-command-prompt/feed/ 0
Explain Like Keyword in SQL with Example https://programmerbay.com/sql-like-keyword-for-matching-a-given-pattern/ https://programmerbay.com/sql-like-keyword-for-matching-a-given-pattern/#respond Sun, 29 Apr 2018 17:20:00 +0000 http://programmerbay.com/?p=3104

LIKE Keyword

There may be a situation where you wanna fetch records from a table that presents a desirable character or word of  your choice in a field, suppose you need all those records in which addresses belong to a particular City, which cannot be grabbed by using assignment operator (‘=’) because an address includes street name,locality, pin code and then City name. To get the address of a specific city name, we can pull it from table by ‘LIKE’ keyword.
For Example:-

| NAME |                 ADDRESS                       |
| xyz       |  1-A,satluj Road,Janakpuri, Delhi    |

You cannot fetch it by = operator:
SELECT * FROM TableName WHERE address =’DELHI’;
For this LIKE operator is used.
LIKE keyword is used to find out all those rows that satisfy a  matching patterns in a field.
In Other word, LIKE keyword’s job is to find out a given pattern in a particular field.
However, it doesn’t do the job all alone, a wildcard comes up with it.
A wildcard provides flexibility to find out any word or set of words from a field in a table.

There are basically two wildcards :

1) %  (percentage sign) :– It is used in place of unknown sequence of characters .
2) _ (underscore sign):– It is  used in place of only a single not known character.

Both can be used at start, middle and end of the given sequence of characters.

Patterns Length Use
     ‘xyz%’ Can be of anything It gets all those sentences or words that start with ‘xyz’
     ‘%xyz’ Can be of anything It gets all those sentences or words that end up with ‘xyz’
  ‘%xyz%’ Can be  of anything It finds all the sentences or words in which ‘xyz’ in between
 “x%z” 3 (2 character + 1 Underscore) It finds all the sentences or words in whose first character would be ‘x’ and last character would be z, and anything can have in between these
‘_yz’ 3 (2 character + 1 Underscore) It fetches all the sequence of characters whose first character can be anything and ends with yz
‘xy_’ 3 (2 character + 1 Underscore) It fetches all the sequence of characters whose last character can be anything and starts with xy
‘x_z’ 3 (2 character + 1 Underscore) It fetches all the sequence of characters whose middle character can be anything that is in between x and y

Note: Underscore stands for only a single character while percentage can be any number of characters


Syntax:
SELECT columns,columns1… FROM tableName WHERE Column LIKE ‘Pattern’;

For Example:

Suppose I have the following table:-

like 2Bquestion

1. Find all those records whose addresses which belong to DELHI

Query:  SELECT * FROM employees WHERE address LIKE ‘%delhi’;

like 2Banswer 2B1

2. Find all those records whose addresses consists of XYZ
Query:  SELECT * FROM employees WHERE address LIKE ‘xyz%’;

like 2B4

3. Find all records whose name’s first character can be anything but ends with ‘ohn’
Query:  SELECT * FROM employees WHERE address LIKE ‘_ohn’;

like 2B3

4. Find all records whose name’s last character can be anything but starts with ‘joh’
Query:  SELECT * FROM employees WHERE address LIKE ‘joh_’;

like 2B5

 

]]>
https://programmerbay.com/sql-like-keyword-for-matching-a-given-pattern/feed/ 0
Check a Column Contains NULL or Empty using WHERE Clause in SQL https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/ https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/#respond Sun, 29 Apr 2018 11:12:00 +0000 http://programmerbay.com/?p=3105

What is NULL?

NULL represents an undefined. It cannot be counted as an empty string (‘ ‘) or zero(0), it’s way different from these. 
Sometimes, It’s not necessary to fill up each and every field with data/information, whenever be a record is inserted or updated. If a record is inserted only with targeted fields only (leaving other fields ), then automatically that particular field/column would get NULL value. 

It can be avoided by using NOT NULL constraint that would prevent from entering  NULL value.
Moreover, we cannot compare two NULL values with one other using operators. 

How to know which field contain NULL value ?

For this, SQL provides us two operators ,  ‘IS NULL’ and ‘IS NOT NULL’ to know whether a particular column consists a NULL Value or not.

IS NULL

It will return all the records which has NULL values in its fields. 
Syntax: Select Col1,Col2,… FROM table WHERE Col  IS NULL;

IS NOT NULL

It will return all the records which hasn’t NULL values in its fields.
Syntax: Select Col1,Col2,… FROM table WHERE Col  IS NOT NULL;

Check a column Contains NULL or Empty String using WHERE Clause in SQL?

Suppose there is a table named ‘customer’ and we want to find all the records of customers having “lastname” column containing null or empty string in the table.
demo table
Query :
SELECT * FROM   customers WHERE  customer_lastname IS NULL OR customer_lastname = ''

Output:

output

About Where Clause

In SQL, WHERE Clause is used to specify a condition and fetching the data from the given table. whether it is single table or by joining of multiple ones. If the condition is satisfied, the result comes out with specific values. So we should use the WHERE clause to filter the records and extract the results that fulfilling specific condition.
In SQL the WHERE clause not only used with the SELECT statements but also with the UPDATE, DELETE statements etc. for getting small clues, some examples/syntax are here to get through but for gaining more about it, we would examine these in next subsequent posts.

Syntax:

SELECT column1, column2, columnN
FROM table_name
WHERE condition;

For Example:

From an Extract of the table data which shows the List of Customers of different Countries and we filter out for some let’s say “India”. So, the Problems become is “List of Customers in India” and there columns contains ID, Name, Product, City, and Country. let’s find out how it filters:


1. SELECT ID, Name, Product, City, Country
2. FROM Customers
3. WHERE Country = ‘India’

Output:

table



There you go “Result” in before your eyes.

There are many Operators that can be used in WHERE clause as:
< > not equal
= equal
> grater than
< less than
LIKE, BETWEEN & IN are also the Operators which can be used in WHERE clause for searching a pattern, Between an inclusive range & to specify the multiple values for a column respectively.

 

]]>
https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/feed/ 0
Explain AND, OR & NOT Operator in SQL https://programmerbay.com/logical-operators-in-sql-for-logical-statements-and-or/ https://programmerbay.com/logical-operators-in-sql-for-logical-statements-and-or/#respond Sun, 06 Aug 2017 16:14:00 +0000 https://www.programmerbay.com/logical-operators-in-sql-for-logical-statements-and-or/

To make WHERE Clause function, operators are used for the purpose so that one can fetch data from the database server.

In this we can focus only on logical operators, a logical operator in SQL is used for comparing to conditions or expressions so that a specific information can fetch out from a table. These logical operators are AND,OR and NOT.

AND Operator

When two or more than two expressions together are said to be true then only those rows would be fetch out that met AND condition.[All condition must be true]

For Example: Suppose you want to draw all the data whose first name starts with cooper and must be male from the table named ‘user_details’ .

SELECT * FROM user_details WHERE first_name=’cooper’AND gender=’male’

and

Explanation:
So, AND operator only grabs those rows from a table that satisfy both expressions as shown above it fetched only those data where the first name was cooper and gender was male as well.

OR Operator

When either of the condition is true then those rows will be fetched out from the table.[One of the condition must be true]

For Example:You require to obtain the data where either the first name could be cooper or gender could be male.

SELECT * FROM user_details WHERE first_name=’cooper’ OR gender=’male’

or

Explanation:
So, OR operator only grabs those rows from a table that satisfies one of the expression as shown above it fetched those data where either the first name was cooper or gender was male.

 

NOT Operator

When a condition or expression is not same as other one then this NOT operator let you to fetch those results.
For Example:You doesn’t want those data where gender is male and first name is cooper.

SELECT * FROM user_details WHERE NOT first_name=’cooper’ AND NOT gender=’male’

not
Explanation:
So, NOT operator only grabs those rows from a table that satisfies both the expression as shown above it fetched those data where the first name was not cooper and gender was not male as well.

]]>
https://programmerbay.com/logical-operators-in-sql-for-logical-statements-and-or/feed/ 0
What are the types of SQL commands with Example ? https://programmerbay.com/sql-a-language-for-fetching-data/ https://programmerbay.com/sql-a-language-for-fetching-data/#respond Mon, 10 Jul 2017 16:49:00 +0000 https://www.programmerbay.com/sql-a-language-for-fetching-data/

SQL is a query language that is very popular nowdays for accessing, modifying and retrieving data from database. It stands for structured query language. Each and every organisation has been investing a decent amount of money to maintain their data in structured way that is on database.

In short, we use SQL language to access data from database (A database may contain so many tables and those tables are used for storing row data and figures in the form of columns and rows in meaningful way).

Lots of Relational database management system uses SQL such as My SQL Server,Oracle and  MS SQL server and so on.

RDBMS is the basis for SQL, and for all the modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL and Microsoft Access.

A Relational Database Management System (RDBMS) is a database management system is based on relational database and firstly it was introduced by E.F Codd.

Today all the RDBMS (MySQL, Oracle, Sybase, MS Access) uses SQL as the Standard Database Language and SQL is used to perform all types of Data Operations in RDBMS.

It is not case sensitive but its better to write all keywords in capital (like SELECT,DATABASE ) to make the queries more readable and clean. Like other languages, to execute multiple queries at a time, each query should end up with semicolon(;).

Some SQL Advantages :-

Easy to learn:-

It is easy to understand and learn as its all keywords and syntax are alike to English  words.

Not case Sensitive:-
Unlike other programming languages such as JAVA and C, SQL is not case sensitive which  means queries can be write and execute properly ,despite of considering uppercase or lowercase.

Communicate with database:-

It is used to create, update,delete and manipulate tables, data and database as well.

Types of SQL commands with Example ?

  1. DATA DEFINITION LANGUAGE (DDL)
  2. DATA MANIPULATION LANGUAGE (DML)
  3. TRANSACTION CONTROL LANGUAGE (TCL)
  4. DATA QUERY LANGUAGE (DQL)
  5. DATA CONTROL LANGUAGE (DCL)

 

  • DATA DEFINITION LANGUAGE (DDL)

It is a standard that defines the different structures in Database.
These statements Create, Modify and removes the database objects such as table, indexes and users.
for Ex.-CREATE, SHOW, ALTER and DROP.

  • ALTER Command with Usage :

Alter statement is used to change the structure of a table. It can be associated with adding a new column, deleting and modifying an existing column .

Syntax:

ALTER TABLE table_name
ADD new_columnName / MODIFY  COLUMN columnName dataType / DROP COLUMN columnName;

Add a column to existing column :

Suppose there is a table name customers and we require to add a column customer_name.

ALTER TABLE customers
ADD customer_name varchar(255);

alter table

In same way, the command can be used for modifying and deleting an existing column

  • DROP Command with Usage: 

Drop statement is used to get rid of a specific table and its associated data / information from a database. Before Dropping any table, always keep in mind that information would also  be gone along with the table.
It is good to have backup of a table or a database, so that it can be recovered whenever a mistake is happened.

Syntax:

DROP TABLE tableName;

For Example, Suppose there is a table named ‘user’ within a database named ‘test’ and I have to delete it completely from  the given database.

drop 2B1

 

 The Statement would be:

1. First, get into the respected database that is ‘test’.
2. After, Drop that table.

USE test                         // To get into test database
DROP TABLE user;  // Deleting the table

drop 2B2
  • CREATE Command with Usage :

The CREATE Table statement is used to create table in database to store the data. the columns define the integrity constraints like Primary key, Secondary key, Unique key, Foreign key while creating tables.

Syntax  

CREATE TABLE
table_name (
 column1 datatype(Size) Constraints,
 column2 datatype(Size) Constraints,
 column3 datatype(Size) Constraints,
 ........
columnN datatype(Size) Constraints
)

CREATE TABLE keyword just simply tells the RDBMS that you wanna create a table for your data.
A table must has a name that must write next to the CREATE TABLE keyword.
Come to the parentheses part in this you mention your columns or fields along with its datatype(A datatype means the type of data you wanna insert in a particular table like for numbers you would prefer int) and the size of the data. Lastly, after giving the size you can specify constraints which are primary key, unique key and not null.

For Example, Suppose you need to create a table named Customer with columns named ID, customer_name, phone number. ID must be a primary key and other fields must not be null.

CREATE TABLE customer(ID INT(10) PRIMARY KEY, customer_name VARCHAR(20) NOT NULL,phone_number VARCHAR(20) NOT NULL);

Output:

create%2Btabe

 

Explanation:

In above example, create table statement used to form the table called ‘customer’ and within parentheses we put in fields such as ID, customer_name and phone_number with datatype.In the table, ID declared as primary key.

 

  • Show Command with Usage :

Show statement can be used for getting the number of databases reside within a DBMS along with the database names. This is the syntax:

For Example: SHOW DATABASES;

Output:

mysql show database


It will return the name of the databases which are currently in  the SQL server. In above example, 6 databases were returned.

It is also used to know how many tables are there within a particular database. For this purpose the syntax is: SHOW TABLES;

For Example:- 

USE test;
SHOW TABLES;

Output:

mysql show tables

Similarly,  In above example , first we had selected a database by using ‘USE’ query and after that we had used the show tables query.

 If you curious about the number of columns of the table along with their names then show statement would come into play. The syntax is

Syntax:-

SHOW COLUMNS FROM table_name;


For Example:

USE test;
SHOW COLUMNS FROM j;

Output:

mysql show columnns

It will return all the columns of the table.

4.  To know about each and every related information of a particular table such as name of the table, version, number of rows and columns and  their format and so many other things about table. The syntax is: SHOW TABLE STATUS FROM table_name;
For Example:
SHOW TABLE STATUS FROM j;

Output:

mysql show table status

 

 

  • DATA MANIPULATION LANGUAGE (DML)

It is used to retrieve, store, modify, delete, insert and data update data in database.
for Ex.- UPDATE, INSERT.
  • INSERT COMMAND with Usage:

The SQL’s INSERT INTO statement is used to add new records to a table in database.

Syntax
There are two possible ways to write the INSERT INTO statement which are shown in below:
INSERT INTO table_name (column1, column2, column3..........)
VALUES (value1, value2, value3,.........);
Here, (columns; 1,2,3,..) are the names of the column in the table.
If we are adding the more values in the table, we may not need to specify the column’s name in the SQL query. so however we have to make sure that the order of the values in the same order as the columns in the table. so, the INSERT INTO syntax is as follows:
INSERT INTO table_name
VALUES ( value1, value2, value3,........)

 

Insert 2BInto

You can create a record in the CUSTOMERS table by using the second syntax as shown below :

INSERT  INTO CUSTOMER
VALUES (5, king sachin, 1122)


All the above statements will produce the following records in the CUSTOMER table as shown below :

Insert 2BInto1

 

POPULATE ONE TABLE USING ANOTHER TABLE :

we can populate the data into a table through the select statement over another table

Syntax

INSERT INTO first_table_name (column1, column2,.....)
SELECT column1, column2......
FROM second_table_name
(WHERE condition);
  • UPDATE Command with usage :

Update command is used to change or update existing records in a table. In this, WHERE clause can also be used to update some particular rows.

Syntax: 

UPDATE table_Name

SET column_name_1 = some value, .. .. .. column_name_N = some value WHERE condition

For Example :

The is a table named customers and we need to update customer_lastname where NULL value exist

Update Statement
  • TRANSACTION CONTROL LANGUAGE (TCL)

TCL Commands are used to manage transactions in database. It allows statements to be grouped together into logical transactions and also managed the changes made by DML statements.
For Ex.- COMMIT, ROLLBACK.
COMMIT : It saves transactions to the database
ROLLBACK : It reverts back all the transactions to the last committed state
  • DATA CONTROL LANGUAGE (DCL)

DCL is a syntax which is similar to a computer programming language used to control and access the data stored in a database.
For Ex.- GRANT, REVOKE
GRANT : It is used provide access to a user for a database
REVOKE : It does opposite, it takes back the permissions from existing users of a database
  • DATA QUERY LANGUAGE (DQL)

It is a Database Language to search for information. It is also referred as Filtering the data. For Example SELECT .
  • Select Command with Usage :

In SQL, SELECT statement is not just for selecting one or more fields but also allowing us to pick up specific and desired information from one and more fields with the help of Where clause, RegExpr, distinct and many more for avoiding irrelevant data.

Here are some basic uses of Select statement.

1. It is used for getting all the table’s information from one or more tables.

Syntax:

SELECT * FROM table_name;


For Example
Suppose there is a table called customers and we want to know how many columns are there in that table along with its data. For this we use:

SELECT * FROM Customers;

Output:

select

 

 
Explanation: 
So, ‘SELECT’ is for selecting columns from the table. Come to the ‘*’ (asterisk) sign ,it simply means ‘all the columns’ (we  will discuss it later) while ‘From’ represents ‘from which table you want to access data’ that is write down to its next.
2. It is also used for getting information of one or more specific columns from a table.
Syntax: SELECT column_name,coulmn_name2 FROM table_name

For Example:
 So, consider you want to extract Name and City from Customer table.For that purpose:

SELECT Name,City FROM Customers    /*If you want to select more than one column then you need to separate each Field With Comma’,’*/

 

select2

 

Explanation:
Same as above except in the we can specify one or more column name (to specify more than one columns , a separator is used that is ‘,’).
3. It’s interesting to know that this clause can compute two mathematical expressions.
Syntax: SELECT expressions;

select3

 

Explanation: 
Rather than just selecting columns from the table it is also used to compute some mathematical calculations such as addition, subtraction,division and multiplication.

]]>
https://programmerbay.com/sql-a-language-for-fetching-data/feed/ 0