Professional Documents
Culture Documents
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL). 1.The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database
2.The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys),specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
Creating Table
How to create a table in sql create table tblstudent ( sid int not null primary key, name nvarchar(50) not null, class nvarchar(50) not null,
university nvarchar(50) not null) create table tblsales ( saleid int not null primary key, quantity int not null , price int not null, type nvarchar(50) not null, sid int null ) Note:if u dnt put not null or null it will b "null" by default
Alter
Alter table tblsales add team nvarchar(50) not null Note:like this wise we can create multiple columns without going into table design
Alter table tblsales drop constraint my-pk(name of primary key set by default or created by u)
Foreign key
Alter table tblsales add constraint my-fk foreign key my-fk foreign key(sid) references tblstudent(sid)
Note:These are the things that should b in mind about prmary key Primary key and foreign key should have same data type 1.primary key cannot b null 2.we cannot delete data in primary for which there is some data in his F.k for deletind the dada we,ll first delete it from F.k and hen from p.k 3.we cannot update data in f.k which does not exit in p.k because each value of f.k key comes from p.k 4.primary key cannot b null but foreign key can b null
select statments
select identifies a column.it retrieves data from a column. select column name
from table name SELECT DISTINCT Statement in a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values.
SQL WHERE Clause The WHERE clause is used to filter records. The WHERE Clause The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syntax SELECT column_name(s) FROM table_name WHERE column_name operator value
SQL uses single quotes around text values (most database systems will also accept double quotes). However, numeric values should not be enclosed in quotes.
With the WHERE clause, the following operators can be used = <> > < >= <= Equal Not equal Greater than Less than Greater than or equal Less than or equal Between an inclusive range
BETWEEN LIKE IN
SQL AND & OR Operators The AND & OR operators are used to filter records based on more than one condition.
The AND & OR Operators The AND operator displays a record if both the first condition and the second condition are true. The OR operator displays a record if either the first condition or the second condition is true. AND Operator Example The "Persons" table: p-id First name Last name Address 1 Zeeshan Ahmad St#8 2 Abdullah Mirza Fff 3 Mussayub Butt Aaa Now we want to select only the persons with the first name equal to zeeshan AND City gujranwala Guj guj
City=gujranwala We wil use this query SELECT * FROM Persons WHERE FirstName='zeeshan' AND city=gujranwala
The ORDER BY Keyword The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sorts the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.
SQL ORDER BY Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
Like,in,between