Everything related to SQL - Part 1

Basics:
Create table:
create table (field1 datatype, field2 datatype,
field3
datatype…. So on);
Example:- create table user(id int, name varchar(255), surname
varchar(255));
Insert records in table:
Insert into (field-names) values (actual values);
Example: insert into user(id, name, surname) values (1, ‘Google’,
‘User’);

Alternative way:
Insert into user values(1, ‘Google’, ‘User’);
Show all records from table:
select * from user;         //Shows all records with all values
select name from user;        //Retrieves the only name from all
records
select id from user;                //Retrieves only id column from all
records
select surname from user;
//Retrieves only user column from all records
Create table with non-null values:-
Syntax: create table (fieldname datatype not null);
Example:
create table no_null_values_allowed(
id int not null,
name varchar(255) not null
);
How to assign default values to a column?
Syntax:
create table (field1 datatype not null DEFAULT
);
create table studentinfo(
Id int not null default 100,
Name varchar(255) not null default ‘anonymous’);
There can be as many as default values as required.
How to use alter command?
Alter table drop column column_name;
This command can be used to delete a specific column from the table
For e.g. alter table studentinfo drop name;
This command can also be used to add new columns
Alter table ADD
;
What is the primary key in
SQL?
The primary key constraint uniquely identifies a
tuple/record from the database.
Primary key contains unique values, and they
don’t accept null values.
How to declare a primary key in a
SQL Table?
create table (field_name
datatype(size) primary key);
create table username(emailid varchar(255) not
null primary key);
create table username(emailid varchar(255) not
null, name varchar(255), lastname varchar(255),
PRIMARY KEY(emailid));
Auto-increment in SQL:
Auto-increment allows a unique number to be
generated for each new record being inserted.
Example:
create table (fieldname datatype not null
auto_increment);
CRUD Operations in SQL:
  1. Create
  1. Read
  1. Update
  1. Delete
  1. Create
Create table student(
id int not null auto_increment,
Name varchar(255),
Surname varchar(255),
Age int,
Primary key(id));
  • We will insert data in it:
Insert into student(name, surname, age)
values
(‘Ria’, ‘Jadhav’, 21);
Insert into student(name, surname, age)
values
(‘Rony’, ‘Mathews’, 20);
Insert into student(name, surname, age)
values
(‘Abhishek’, ‘Patil’, 19);
  1. For reading(Select statement)
Select * from student;
To read we can use “WHERE CLAUSE”
Select name, surname from student where age=21;
Result:
Name
Surname
Rony
Mathews
  1. Update Statement
UPDATE students SET age = 23 WHERE name =
“Rony”;
This command will change the age of Rony from
21 to 23
Updated result:-
Name
Surname
Age
Rony
Mathews
23
  1. Delete
Delete from student where name = “Rony”;
The record where the name is Rony will be deleted.
Part-2 coming soon...
Word of the day:
Anticipate
Meaning:-
To see what might happen in future and take actions on it
Sentences:-
1.  Mary anticipates that’s why she deals with her problems easily
2. Weather forecasters take help of AI tools for anticipation
Synonyms:
Foresee, predict, foretell, expect, await
Antonyms:
Doubt, be amazed, be surprised, dread, cower at
For more follow: botgre

Comments