create account

Structured Query Language(Basics 1) by gunu1

View this thread on: hive.blogpeakd.comecency.com
· @gunu1 ·
$2.03
Structured Query Language(Basics 1)
<div class=text-justify>Hello, there it's me once again (obviously), but I've come bearing gifts of knowledge. This time around I'm here with the Structured Query Language AKA SQL. It is a language of database systems. SQL is what's used to store, manage and manipulate data stored in a database.

A lot of times people tend to wonder how all the stuff they see online is stored, it's not like whatever that is online is being retrieved from a paper secured in a vault somewhere underground. Let's start from the basics, a database is a collection of related data, so a database can be your cardboard, your suitcase, or anywhere you put files alike.

This means there are many database types depending on a lot of topics, which could be ease of access, security purposes, or any other. The most popular one of the ages is the traditional file system. Just as the name suggests it's writing records in files.

The current database trend is relational database systems, which represent data in a tabular form, just as you have rows and columns in a table. If you have a database, it serves as a container, inside it is what is called tables, these tables are used to cluster relative records, this table comprises Rows and columns. 

A row is a full record and a column is an attribute of the record or a piece of the record. Just as you Would have a person's full name as a whole record, you would have to divide it into First Name and LastName, right? 

<hr/>

<center>**The Syntax and Basics**</center>

<hr/>

Now that we have an Idea of how A database is, head to the basics of SQL, one thing to take note of is that SQL is just like the English you speak with just a slight difference. The first step you need to choose the Database Management System you wish to, for this piece I will use Microsoft SQL Server with Microsoft SQL Management Studio and the syntax will be in Microsoft SQL format. 

The management system you choose to use affects how you'll write certain statements, don't worry much its just some slight difference. The first thing to note is that we mostly write the reserved words of SQL in BLOCK, not that it's compulsory to do, just that it makes it easy for documentation and helps a lot.

So let's get down to business, if you need a database you definitely have to create one right? We use the <strong>CREATE DATABASE</strong> command followed by the name of the database we wish to have.

Example:
<code>
CREATE DATABASE SQLclasss
</code>

My management studio looks like this:

<div class=pull-left>
https://images.hive.blog/DQmYiKr7VL8ATqgnCfTBQHkLky1w5Uiq3iNh5f7DUyEjhRA/001.PNG
<sub>Take a look at my databases present</sub></div> <div class=pull-right>
https://images.hive.blog/DQmYZMMsaxsU7Pzk1Rz5UxyoLnYYD3wZ9CZZsVMx88CprCB/002.PNG
<sub>A success after the execution</sub></div>

<hr/>

<center>
https://images.hive.blog/DQmaUy3mi1wybJYpymjZYT6u67tumu3Q4kzLc2M4n4GqQBf/003.PNG
<sub>Added to my databases</sub></center>

<hr/>

Since it is MS SQL, mostly when you open the management studio, you'll find that the master Database is selected by default, so after using to create the database, you then select the database you wish to use by using the <strong>USE</strong> command, followed by the name of the database.

Example:

<code>
USE SQLclasss
</code>

After execution:

<div class=pull-left>
https://images.hive.blog/DQmcNkq8ZhgrFbVe7C6p6r7PawY3WWtnncTToxiW6TTYUhL/004.PNG
<sub>Note</sub></div> <div class=pull-right>
https://images.hive.blog/DQmU1ZC6juMBGYmaHJMpHs7aeRcVGDyVyKV36xrdhffoCAz/005.PNG
<sub>A success after the execution</sub></div>

<hr/>

The next step is the commands for creating, and manipulating our tables. This is called CRUD a short form for Create, Read, Update, Delete. This means commands for creating tables, retrieving them, updating records, and finally deleting them.

<hr/>

<center><sub>**Create**</sub></center>

<hr/>

Creating a table has a key factor, which is what it will entail, I'm talking about the attributes. The attributes are things that describe the data to be kept there, meaning, for instance if your table is supposed to be a table that will take the full name and age of people, then you could have a table like this:

**First Name**
**Last Name**
**Age**

So that would mean the table has three attributes, First Name, Last Name, and Age. One thing to note is that attributes should have a data type and size, meaning, If you need something that will take Words or lists or anything that has alphabets, then you'd need a data type that will allow it.

<hr/>

<center>**Some Data Types**</center>

<hr/>

<strong>VARCHAR</strong> : Used for characters or strings
<strong>CHAR</strong> : Used for a character with a fixed length
<strong>INT</strong> : Used for Integer numbers
<strong>FLOAT</strong> : Used for floating point number or decimal numbers 
<strong>DATE</strong> : Used to store date
<strong>TIME</strong> : Used to store time
<strong>DATETIME</strong> : Used to store both time and date

There are a whole lot of data types out there for different purposes which will always come in handy, for now, I'll just be using these few. Now, let's get back on track, to create a table, we need a name for it, the columns to be used and their data types, this is for the start, we can add what's called constraints to it too but let's leave that for now. 

Let's create a table in our SQLclass database with these columns or attributes: First Name, Last Name,  and telephone number of our friends. One thing to note is that the first three columns are going to be made of alphabets right? so why not use <strong>VARCHAR</strong> for it? and us <strong>INT</strong> for the telephone number? this is how we execute it:

<code>
CREATE TABLE friends(
FirstName VARCHAR(50),
LastName VARCHAR(50),
Telephone INT);
</code>

<i>Note: The syntax from above, and the Columns are kept in brackets and each column is followed by a data type and the size in parenthesis, the size means how many characters it can hold. VARCHAR(50) means 50 characters</i> Also, note how I highlight the command I wish to execute and then tap execute, this is because there are various commands there, without highlighting and executing it will execute all commands sequentially.

And the comment too, will not be executed no matter what, unless, of course, you take away the "#", this is used for documentation.

<div class=pull-left>
https://images.hive.blog/DQmekGnXiWMau4UGysPhanndiABL6kDRLNhqAzySahAKZq7/006.PNG
<sub>The tables in the Database before execution</sub></div> <div class=pull-right>
https://images.hive.blog/DQme4fa1opkqeQikEcdqDopNLUZLj6efHokddsHoENMB639/007.PNG
<sub>After execution</sub></div>

<hr/>

The syntax: CREATE TABLE your_tabel_name_here(
column_name_here DATA_TYPE_HERE,
column_name_here DATA_TYPE_HERE,
column_name_here DATA_TYPE_HERE);

"CREATE TABLE" is the first command, followed by your table name then put in brackets the Names and Data types of your columns. Use the example I've implemented as a reference point.


<hr/>

<center>**Inserting**</center>

<hr/>

Since we have a table, why don't insert some data into it? To insert we need to know the columns we're inserting into, we could also decide to insert into all columns.

To insert into our table, we do this:

<code>
INSERT INTO friends(FirstName,LastName,Telephone) values(
'Abdul Kahar',
'Abdul Rahman',
0123456789);
</code>

Note that <strong>INSERT</strong> and <strong>INTO</strong> are keywords, meaning they possess their own purpose in the language hence you can not use it for any other purpose than what it's meant for.

The end results:

<center>
https://images.hive.blog/DQmSyzvRZnDkkKBPjB26t1rXnx2Cv8EFjYRRoMYfg6ofZbn/008.PNG
</center>

Make sure to take note of how you arrange the columns in the INSERT command, also, whatever you are writing that has to do with characters should be quoted just like the "Abdul Kahar".

<hr/>

<center>**Read and Update**</center>

<hr/>

So let's say the data we took and inserted has an issue that we need to update a column with a different value or whatever. The syntax is very simple:

<code>
UPDATE frinds set FirstName='Abdul Kahar Gunu' where Telephone=0123456789;
</code>

Firstly let's see what in our 'friends' table this command:

<code>
SELECT * FROM friends
</code>

<center>
https://images.hive.blog/DQme4m25hWgq2JizqmisdxHeshubuEBxXQZzH29gqogEW2a/009.PNG
<sub>Retrived data</sub></center>

<hr/>

The above command is what's used to read data in a table, the * there means all, meaning the whole command says"SELECT all FROM friends" makes sense right? This also means we could retrieve only one record or two or however many we wish for. To retrieve specific records, we would have to add a condition so it'll help in the search for the record(s).

Check out this command

<code>
SELECT * FROM friends where = Telephone=0123456789;
</code>

Yeah, it means we retrieving all columns of the record which has its Telephon= 0123456789 . We could retrieve a column of more and even specify how we wanted them to be presented:

<code>
SELECT LastName FROM friends where = 0123456789;
</code>

<center>
https://images.hive.blog/DQmcMPHKYoYVeUH2vDYaB7sHDG96MgRPw9T6KXYAdHUTd7s/010.PNG
<sub>Retrived data</sub></center>

See the column name above? we could also do something like:

<code>
SELECT Telephone,FirstName FROM friends WHERETelephone = 0123456789;
</code>

<center>
https://images.hive.blog/DQmWZh9stvNmJFD2XfChqejf5HQBZEPWFcyfVJqGhha2JmM/011.PNG
<sub>Retrived data</sub></center>


<hr/>

Let's get back on track to Updating a record, our command was

<code>
UPDATE friends set FirstName= 'Abdul Kahar Gunu' where Telephone=0123456789;
</code>

It means we are updating the column "FirstName" and changing it to "Abdul Kahar Gunu" and the condition is a record(s) that possess a "Telephone" number = 0123456789

<div class=pull-left>
https://images.hive.blog/DQmQXAvSYt1CtdsKXSUFcsw2oxNFZYAXuR1NNY4qzbK45di/012.PNG
<sub>Sucess</sub></div> <div class=pull-right>
https://images.hive.blog/DQmXfUrXzULFaVb2m4zhWv1xeFhturvJ3zA4o3eqNBmThAF/013.PNG
<sub>After execution</sub></div>

<hr/>

<center>Delete</center>

<hr/>

Let's delete something from our table, right? Firstly, let me add another record... Done lets gooooooooo. Just as we updated our record, it is basically the same just a slight change:

<code>
DELETE FROM friends WHERE Telephone = 1234567890
</code>

See the keywords? the condition? yeah, simply ain't it?

<div class=pull-left>
https://images.hive.blog/DQmUMfvTJ2UjmT8WgQG7pSjqYhqvnqajaWZKAbV9XUVWcLM/014.PNG
<sub>The recoreds</sub></div><div class=pull-right>
https://images.hive.blog/DQmbZPqHDvCz1h9NB4HBNW9bzzUWJSHZVmZ7XZwidfs18fF/015.PNG
<sub>After execution</sub></div>

<hr/>

SQL is quite simple once you put your mind to it, I have tried to dumb it down somehow, If you do not understand any part you can reach out to me. If you want I make a recording and explain it properly. This is just the beginning of it, this is just some bit of concepts and with time we'll to moving to more concepts.
 
<center>Thank you for your time</center></div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 142 others
properties (23)
authorgunu1
permlinkstructured-query-languagebasics-1
categoryhive-169321
json_metadata"{"app":"peakd/2022.11.2","format":"markdown","description":"This is tutorial is about SQL, I have explained a few basic topics of it.","tags":["programming","sql","database","mssql","mysql","software"],"users":[],"image":["https://images.hive.blog/DQmYiKr7VL8ATqgnCfTBQHkLky1w5Uiq3iNh5f7DUyEjhRA/001.PNG","https://images.hive.blog/DQmYZMMsaxsU7Pzk1Rz5UxyoLnYYD3wZ9CZZsVMx88CprCB/002.PNG","https://images.hive.blog/DQmaUy3mi1wybJYpymjZYT6u67tumu3Q4kzLc2M4n4GqQBf/003.PNG","https://images.hive.blog/DQmcNkq8ZhgrFbVe7C6p6r7PawY3WWtnncTToxiW6TTYUhL/004.PNG","https://images.hive.blog/DQmU1ZC6juMBGYmaHJMpHs7aeRcVGDyVyKV36xrdhffoCAz/005.PNG","https://images.hive.blog/DQmekGnXiWMau4UGysPhanndiABL6kDRLNhqAzySahAKZq7/006.PNG","https://images.hive.blog/DQme4fa1opkqeQikEcdqDopNLUZLj6efHokddsHoENMB639/007.PNG","https://images.hive.blog/DQmSyzvRZnDkkKBPjB26t1rXnx2Cv8EFjYRRoMYfg6ofZbn/008.PNG","https://images.hive.blog/DQme4m25hWgq2JizqmisdxHeshubuEBxXQZzH29gqogEW2a/009.PNG","https://images.hive.blog/DQmcMPHKYoYVeUH2vDYaB7sHDG96MgRPw9T6KXYAdHUTd7s/010.PNG","https://images.hive.blog/DQmWZh9stvNmJFD2XfChqejf5HQBZEPWFcyfVJqGhha2JmM/011.PNG","https://images.hive.blog/DQmQXAvSYt1CtdsKXSUFcsw2oxNFZYAXuR1NNY4qzbK45di/012.PNG","https://images.hive.blog/DQmXfUrXzULFaVb2m4zhWv1xeFhturvJ3zA4o3eqNBmThAF/013.PNG","https://images.hive.blog/DQmUMfvTJ2UjmT8WgQG7pSjqYhqvnqajaWZKAbV9XUVWcLM/014.PNG","https://images.hive.blog/DQmbZPqHDvCz1h9NB4HBNW9bzzUWJSHZVmZ7XZwidfs18fF/015.PNG"]}"
created2022-12-01 18:43:42
last_update2022-12-01 18:43:42
depth0
children2
last_payout2022-12-08 18:43:42
cashout_time1969-12-31 23:59:59
total_payout_value1.022 HBD
curator_payout_value1.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11,099
author_reputation8,235,892,826,139
root_title"Structured Query Language(Basics 1)"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id118,767,776
net_rshares4,291,861,447,135
author_curate_reward""
vote details (206)
@hivebuzz ·
Congratulations @gunu1! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@gunu1/upvoted.png?202211302150"></td><td>You received more than 300 upvotes.<br>Your next target is to reach 400 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@gunu1) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hive-122221/@hivebuzz/pum-202211-result"><img src="https://images.hive.blog/64x128/https://i.imgur.com/mzwqdSL.png"></a></td><td><a href="/hive-122221/@hivebuzz/pum-202211-result">Hive Power Up Month Challenge 2022-11 - Winners List</a></td></tr><tr><td><a href="/hive-102201/@hivebuzz/wc2022-recap-day11"><img src="https://images.hive.blog/64x128/https://i.imgur.com/uNPMgnd.png"></a></td><td><a href="/hive-102201/@hivebuzz/wc2022-recap-day11">HiveBuzz World Cup Contest - Recap of Day 11</a></td></tr><tr><td><a href="/hive-122221/@hivebuzz/pum-202212"><img src="https://images.hive.blog/64x128/https://i.imgur.com/M9RD8KS.png"></a></td><td><a href="/hive-122221/@hivebuzz/pum-202212">Be ready for the last Hive Power Up Month of the year!</a></td></tr></table>

###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/199)!
properties (22)
authorhivebuzz
permlinknotify-gunu1-20221201t184847
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2022-12-01 18:48:48
last_update2022-12-01 18:48:48
depth1
children0
last_payout2022-12-08 18:48:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,645
author_reputation367,965,646,463,563
root_title"Structured Query Language(Basics 1)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id118,767,917
net_rshares0
@stemsocial ·
re-gunu1-structured-query-languagebasics-1-20221202t051202387z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-gunu1-structured-query-languagebasics-1-20221202t051202387z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2022-12-02 05:12:03
last_update2022-12-02 05:12:03
depth1
children0
last_payout2022-12-09 05:12:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,460,334,324,555
root_title"Structured Query Language(Basics 1)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id118,780,762
net_rshares0