introduction to database

31
Introduction to Database

Upload: oly07104

Post on 23-Dec-2014

620 views

Category:

Technology


1 download

DESCRIPTION

Corrected Introduction to database slide

TRANSCRIPT

  • 1. In our programs we use variables to storeinformation. But when our program terminates all thevariables declared in it disappear. Why this happens????

2. Whatif we need some information to bestored even after the termination ofprogram. (We are here in this session forthis need). How can we fulfill this need? Any suggestion.. 3. The most simple solution is store theinformation in the disk. Now the question is how we can store indisk. Create a file and store there.(simpleenough) Is it a good solution or the best solution ? 4. Suppose we want to implement the log insection.(feature 1) We use a file and store the user_id andpassword in the following manner 5. When1105120 wants to log in we have tocheck all the previous record. Also we have to check whether multipleusers are accessing the file. Any more problem. 6. So we need to care about a lot of factsbeside storing, updating the file. But we are lazy people. We always want tominimize our task.(At least me!) Thank GOD there are some applications orSystems which handle the facts aboutwhich we were worried 7. Andthey are called DataBase Management System(DBMS). Oracle IBM DB2 Microsoft SQL Server Microsoft Access PostgreSQL MySQL SQLite 8. But not all our problems have been solved. Why??? Because now we have to learn how tomaintain Database using DBMS . Here we will see how to manage databaseusing MySQL. 9. CREATE DATABASE [IF NOT EXISTS]database_name; Example: Suppose I want to create adatabase named Quiz. So the query will beCREATE DATABASE IF NOT EXISTS QUIZ; 10. SHOWDATABASES; You will see something like this- 11. DROP DATABASE [IF EXISTS]database_name; Example : Suppose I want to delete thequiz database : So the query will beDROP DATABASE IF EXISTS Quiz; 12. Wehave created Database in MySQL. Now we will store data in Database. We store data in Tabular format in MySQL. That means we have to create tables tostore data. 13. CREATE TABLE [IF NOT EXISTS] table_name( column_list) type=table_type Example: Suppose I want to create table named User_Password 14. Query will be: CREATE TABLE USER_PASSWORD( username varchar(6), password varchar(6) );Username(varchar(6)) Password(varchar(6)) 15. To see the created table in the database use query SHOW TABLES; To see the description of a particular table use query DESCRIBE table_name; 16. INSERT INTO tbl_name () VALUES(); Example: INSERT INTO USER_PASSWORDVALUES(0705001,123456); INSERT INTO USER_PASSWORDVALUES(0705002,678901);Username(varchar(6)) Password(varchar(6))07050011234560705002678901 17. Altertable is used to change the structureof the table Suppose we forgot to add a column in theUSER_PASSWORD table. Now we want to add a column name typein that table 18. ALTERTABLE USER_PASSWORD ADD COLUMN TYPE VARCHAR(5);Using ALTER TABLE query: We can also drop column Rename the table Change the properties of a column 19. SELECTcolumn_name1,column_name2... FROM tables [WHERE conditions] [GROUP BY group [HAVING group_conditions]] [ORDER BY sort_columns] [LIMIT limits]; Looking very complex 20. Mostsimple select statement SELECT * FROM table_name; It selects the whole table; SELECT COLUMN_NAME FROM TABLE Example: SELECT USERNAME,TYPE FROM USER_PASSWORD; 21. We do not always want to select the whole table .Most of the time part of a table. Example: SELECT PASSWORD,TYPE FROM USER_PASSWORD WHERE USERNAME = 0705001; 22. Primarykey is used to distinguish two rowsin a table. Now if I execute INSERT INTO USER_PASSWORD VALUES(0705001,hello); Username(varchar(6)) Password(varchar(6)) 0705001123456 0705001hello 0705002678901 23. Bigproblem. One user two passwords. :O We can avoid it by making USERNAMEcolumn as primary key. By making USERNAME column a primarykey we ensure that this column will haveall distinct value. 24. CREATE TABLE USER_PASSWORD(username varchar(6),password varchar(6), PRIMARY KEY (USERNAME) ); 25. Usedto change data in the table. UPDATE table_name SET column_name= value WHERE (Some condition is true); Suppose 0705001 wants to change his/her password. The query will be : UPDATE USER_PASSWORD SET PASSWORD = 1234 WHERE USERNAME = 0705001; 26. Usedto delete data in the table. DELETE FROM table_name WHERE(Some condition is true); Suppose we want to throw out an user(0*0*200) from our Database !!! DELTE FROM USER_PASSWORD WHERE USERNAME = 0*0*200; 27. These were very basic database queries. Dont be worried if you did not understand all the topics. You will learn all this and many other features in 3-1. 28. THANK YOU