Introduction to ArtificiaI Intelligence in Higher Education
Sql introduction
1. ANY USEFUL INFORMATION IS A DATA
IN SIMPLE WORDS DATA CAN BE FACTS RELATED TO ANY OBJECT IN CONSIDERATION
FOR EXAMPLE YOUR NAME, AGE, HEIGHT, WEIGHT, ETC. ARE SOME DATA RELATED TO YOU.
A PICTURE , IMAGE , FILE , PDF ETC. CAN ALSO BE CONSIDERED DATA
What is Data?
What is Database?
A database is a systematic collection of data.
Databases support storage and manipulation of data.
Databases make data management easy
Ex: An online telephone directory
2. WHAT IS A DATABASE
MANAGEMENT SYSTEM (DBMS)?
• Database Management System (DBMS) is a collection of programs which
enables its users to access database, manipulate data, reporting /
representation of data . It also helps to control access to the database.
• Database Management Systems are not a new concept and as such had been
first implemented in 1960s. Charles Bachmen's Integrated Data Store(IDS) is
said to be the first DBMS in history
3. TYPES OF DBMS:
• Hierarchical - this type of DBMS employs the "parent-child"
relationship of storing data. The windows registry used in
Windows OS is an example of a hierarchical database.
• Network DBMS - this type of DBMS supports many-to many
relations. Ex: RDM Server
• Relational DBMS - this type of DBMS defines database
relationships in form of tables, also known as relations Ex:
MySQL, Oracle and Microsoft SQL Server
4. WHAT IS RELATIONAL
DATABASE?
It is the collection of relations or two dimensional tables
Dr E.F Codd proposed the relational model for database system in 1969
Relational Model Consists of following :
• Collections of Objects or Relations
• Set of Operators to act on the relations
• Data integrity for accuracy and consistency.
(Data integrity is the one which contains correct data in the database)
To Preserve data integrity we have two types:
1.DataType: Data Type is a type of data which you store in each column
2.Constraints
5. CONSTRAINTS:
A constraint is a condition which restricts the invalid data in the table.
A constraint can be provided for a column of a table.
Types of Constraints
1. NOT NULL
2. UNIQUE
3. Primary Key
4. Foreign Key
5. Check
6. NOT NULL -
• NULL is nothing, it is neither zero nor blank space. It will not occupy any space in the memory
• NOT NULL will ensure at least some value should be present in a column
UNIQUE -
• It will not allow any duplicates in a column
• UNIQUE column can take multiple NULL (s)
Primary Key
• It is the combination of NOT NULL and UNIQUE
• Only one PK is allowed in a table
• PK identifies a record uniquely in a table
7. Foreign Key
• FK creates relationship between any two tables
• FK is created on the child table
• FK can take both NULL and duplicate values
• We can have more than 1 FK in a given table
CHECK
• It is used to provide additional validations as per the customer requirements.
8. SQL:
• Developed by IBM SQL – Structured Query Language.
• SQL – it is a language to talk to the database / to access the database
• To work on SQL , a DB software (RDBMS) is required.
• SQL is not case sensitive
What is SQL?
Structured Query language (SQL) pronounced as "S-Q-L" or sometimes as
“See-Quel” is actually the standard language for dealing with Relational
Databases. SQL can be effectively used to insert, search, update, delete
database records.
10. SQL STATEMENTS:
SQL SELECT Statement
SELECT column1, column2....columnN FROM table_name;
SQL WHERE Clause
SELECT column1, column2....columnN FROM table_name WHERE
CONDITION;
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000;
11. SQL STATEMENTS:
SQL ORDER BY Clause
SELECT column1, column2....columnN FROM table_name WHERE
CONDITION ORDER BY column_name {ASC|DESC};
SQL GROUP BY Clause
SELECT SUM(column_name) FROM table_name WHERE CONDITION
GROUP BY column_name;
SQL> SELECT * FROM CUSTOMERS ORDER BY NAME, SALARY;
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY NAME;
12. SQL STATEMENTS:
SQL CREATE TABLE Statement
CREATE TABLE table_name( column1 datatype, column2
datatype, column3 datatype, ..... columnN datatype,
PRIMARY KEY( one or more columns ) );
SQL DROP TABLE Statement
DROP TABLE table_name;
SQL>
CREATE TABLE CUSTOMERS
(ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID));
SQL> DROP TABLE CUSTOMERS;
SQL> DESC CUSTOMERS;
ERROR 1146 (42S02): Table 'TEST.CUSTOMERS' doesn't exist
13. SQL STATEMENTS:
SQL TRUNCATE TABLE Statement
TRUNCATE TABLE table_name;
SQL INSERT INTO Statement
INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
SQL > TRUNCATE TABLE CUSTOMERS;
SQL> SELECT * FROM CUSTOMERS;
Empty set (0.00 sec)
INSERT INTO CUSTOMERS VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
14. SQL STATEMENTS:
SQL UPDATE Statement
UPDATE table_name SET column1 = value1, column2 =
value2....columnN=valueN [ WHERE CONDITION ];
SQL DELETE Statement
DELETE FROM table_name WHERE {CONDITION};
SQL> UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;
SQL> DELETE FROM CUSTOMERS WHERE ID = 6;
15. SQL JOINS
• The SQL Joins clause is used to combine records from two or more tables in a
database. A JOIN is a means for combining fields from two tables by using values
common to each.
There are different types of joins available in SQL −
•INNER JOIN − returns rows when there is a match in both
tables.
•LEFT JOIN − returns all rows from the left table, even if there are
no matches in the right table.
•RIGHT JOIN − returns all rows from the right table, even if there
are no matches in the left table.
•FULL JOIN − returns rows when there is a match in one of the
tables.
•SELF JOIN − is used to join a table to itself as if the table were
two tables, temporarily renaming at least one table in the SQL
statement.
16. INNER JOIN:
SELECT table1.column1, table2.column2... FROM table1
INNER JOIN table2 ON table1.common_field = table2.common_field;
SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
INNER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
17. LEFT JOIN:
SELECT table1.column1, table2.column2... FROM table1
LEFT JOIN table2 ON table1.common_field = table2.common_field;
SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
LEFT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
18. RIGHT JOIN:
SELECT table1.column1, table2.column2... FROM table1
RIGHT JOIN table2 ON table1.common_field = table2.common_field;
SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
19. FULL JOIN:
SELECT table1.column1, table2.column2... FROM table1
FULL JOIN table2 ON table1.common_field = table2.common_field;
SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
FULL JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;