SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL OPERATORS SQL COMMANDS SQL Queries

SQL Table

SQL TABLE SQL CREATE TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL UPDATE TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT INTO Statement

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.

CREATE: Creates new database, table, or view of table.
ALTER: Modifies the database or table. MODIFY, ADD keywords are used in altering data like altering table schema or altering database. If user remember the things after creation of table at that time alter command used for modification.
DROP: Deletes entire table with its structure/schema or deletes view of table or other objects of database.

Syntax: CREATE DATABASE Database_name;
Example: Create Database College;
Syntax: CREATE TABLE table_name (column_name1 data type, column_name2 data type,………);
Example: CREATE TABLE STUDENT (student_id int, student_name varchar(50), student_dept varchar(50), classyear varchar(10)); See the output screen carefully, screen prompting MariaDB [test] >, here MariaDB is the database System and test is the name of database in which we are
 performing SQL operations. 


  • DESC: The DESC command shows the structure/ schema of the table. Schema means its structure. In a simple language, ‘it is a blueprint of a table’, which shows table name with its details like how many columns are present in that table, data type of each column means which type of value a column can hold. It shows keys, it shows whether column can hold null value or not, default values and extra information.
Syntax: DESC TABLE_NAME;
Example: Desc student; # Syntax for Creating View
Syntax: CREATE VIEW view_name AS SELECT column 1,column 2,….. FROM Table_name [WHERE condition];
Example: Create view student_view  as Select * from student Where Dept = ’IT’;
Syntax: Alter Table Table_name Add column column_name data type;
Example: Alter Table Student Add Column City Varchar(10); New column ‘city’ is added to the STUDENT table. DESC command shows structure of student table before alteration and after alteration of table. Yet, we have not entered any value to the new column ‘city’. NULL is a default value for each column. City column shows null value for all records present in the table. 

   

Syntax: Alter Table Table_name Modify column  column_name data type constraint;
Example: Alter Table Student Modify Column student_id int NOT NULL;
Schema level modification is done using alter table. Not null constraint is added on column student_id.
Syntax: Alter Table Table_name Add constraint_name (column_name);
Example: Alter Table Student Add Primary Key(student_id,student_name);
 
 
Primary key is added on two columns means combination of student_id and student_name become a primary key for table student.
Syntax: Alter Table Table_name DROP column  column_name;
Example: Alter Table Student DROP Column city; Here ‘city’ column is deleted using alter keyword. Now city column is not present in student table. 

 

Output screen shows the structure of the table before deleting column city and also structure after deletion of city column.
Syntax: DROP Table table_name;
Example: DROP Table Student; Drop command deletes all the data i.e. all records with structure/schema of the table from database.



ADVERTISEMENT
ADVERTISEMENT