SQL Examples
SQL Examples
How to run SQL commands:
Here are some examples...
1. Create Statement - Using create statement , we can create table or database.
- Example: Using create statement for database creation
create database database_name;
- Example: Using create statement for table creation.
create table table_name (column1 datatype, column2 datatype, column3 datatype, ...);
2. Insert Statement - Using insert statement , we can insert data in the table.
There are two ways to insert data in the table using insert statement.
- Example 1: Enter data in the table using insert statement (1st way)
insert into table_name values (value1, value2, value3, ...);
- Example 2: Enter data in the table using insert statement (2nd way)
insert into table_name (column1, column2, column3, ...) values (value1, value2, value3, ...);
3. Select Statement - Using select statement , we can retrieve data from database.
- Example 1: Retrieve data from database using select statement. By using this query, we can retrieve whole data of the table, means all columns will be retrieved.
select * from table_name;
select column1, column2, column3, ... from table_name;
4. Drop Statement - Using drop statement , we can delete table from the database.
- Example: Drop table table_name;
- Verified: Table has been deleted.
5. Truncate Statement - Using truncate statement , we can delete all rows from the table.
- Example: Truncate table table_name;
First, lets check available data in the database
Now, lets use the truncate command
- Verified: Table is empty.
6. Delete Statement - Using delete statement , we can delete rows on the basis of condition. Like if we want to delete a particular rows.
- Example: Delete from table_name where condition;
First, lets check available data in the database
Now, lets use the delete command
- Verified: Available data in the table.
6. Alter Statement - Using alter statement , we can modify the structure of the table. We can also add or drop constraint from the table.
1. Adding column in the existing table
Example: Alter table table_name add column column_name datatype;
- Checking existing table.
- Verified : Updated table
2. Deleting column from the existing table.
Example: Alter table table_name drop column column_name;
- Checking existing table.
3. modifying column name in the existing table.
Example: Alter table table_name rename column old_column_name to new_column_name;
4. Modifying column data type in the existing table.
Example: Alter table table_name change column column_name type new_datatype;

Comments
Post a Comment
If you have any doubts, Please let me know