Jumpstart Your Database Journey: A Beginner’s Guide to SQL Commands
Welcome aboard to the thrilling world of SQL commands! Whether you’re a seasoned programmer or just starting out, this is the perfect place to dive into the world of databases.
No need to worry as we’re starting off with the basics so newbies can easily follow along and get hands-on practice as we go. This blog will be a fun and engaging journey into the fundamentals of SQL commands, so get ready to take your first steps on this exciting path!
We’ll start with the creation of database first and will move on to the things like table creating, inserting values, altering table with adding constraints and completing three interesting tasks.
Command in the line no.1 is to used to create the database and is named HRDM_2. GO in line 2 is used for the execution (not neccessary). Command in the fourth line is used to create the table named employees_data with 5 colums defined as emp_id, first_name, last_name, salary and hire_date.
INT PRIMARY KEY creates an integer (INT)column that is designated as the primary key for the table. This means that each row in the table must have a unique integer value in this column, and that column can be used as a foreign key in other tables to establish relationships between data.
VARCHAR is the combination of Variable and Character and the number 50 is the limit of characters. Decimal (10,2) is a data type that represents a fixed-point number with 10 total digits, 2 of which are after the decimal point
INSERT INTO command is used to add the values in our table “employee_data” and the VALUES in the line 13 represent the values we would like to add in the table.
ALTER TABLE is to modify the structure of an existing database object, such as a table, view, or index. The purpose of using it here is to add a constraint to check the value of salary in the table is greater than zero.
Lastly, command in the line 19 “ SELECT * FROM employees_data” is used to get the result of the all the commands and the table created above. * is used to get all the complete data.
Next Query: SELECT & WHERE
In this Query database named “nothwind” is used to get all the data from the table where the column named “ContactTitle” is ‘Owner’.
Result is:
Task 1: Inserting Identity Column
To check if identity column is added or not:
Expand the database in the Object Explorer, then expanding the Tables folder and finding the table you’re interested in. Right-click on the table and select “Design” to open the table designer.
In the table designer, you can see the columns of the table and their properties. If an identity column has been added to the table, it will appear with the “Identity Specification” property set to “Yes”.
Task 2: Creating a new table from an existing one
To create a new table based on an existing table, you can use the SELECT INTO
statement. Here's an example:
Here I have created a new table named “new_table” from the table “ Order Details “ using its two tables- ProductID and Quantity.
Result:
Task 3: Inserting data from one table into another
To insert data from one table into another, you can use the INSERT INTO
statement. Here's an example
Complete Order Detail table in northwind database.
You can verify the result from the actual table above by tallying the actual table and the result table.
Congratulations on completing the three tasks and learning some of the most important commands in SQL! I hope this practical learning experience has been valuable for you and that you feel more confident using SQL. By putting your new knowledge into practice, you’ll be able to analyze and manipulate data more efficiently, which is an essential skill for many industries. Keep exploring and practicing, and I’m sure you’ll continue to improve your SQL skills!