Introduction to Structured Query Language (SQL)
Hello friends in this article we will learn Basic concept of database with SQL and learn about SQL query. We also run how we run sql queries with standard syntax. We have learnt about Relational Database Management System (RDBMS) and purpose in the previous chapter. There are many RDBMS such as MySQL, Microsoft SQL Server, PostgreSQL, Oracle, etc. that allow us to create a database consisting of relations and to link one or more relations for efficient querying to store, retrieve and manipulate data on that database. In this chapter, we will learn how to create, populate and query database using MySQL. Let start with Database concepts with SQL
One has to write application programs to access data in case of a file system. However, for database management systems there are special kind of programming languages called query language that can be used to access data from the database. The Structured Query Language (SQL) is the most popular query language used by major relational database management systems such as MySQL, ORACLE, SQL Server, etc. SQL is easy to learn as the statements comprise of descriptive English words and are not case sensitive. We can create and interact with a database using SQL in an efficient and easy way. The benefit with SQL is that we don’t have to specify how to get the data from the database. Rather, we simply specify what is to be retrieved, and SQL does the rest. Although called a query language, SQL can do much more besides querying. SQL provides statements for defining the structure of the data, manipulating data in the database, declare constraints and retrieve data from the database in various ways, depending on our requirements. In this chapter, we will learn how to create a database using MySQL as the RDBMS software. We will create a database called StudentAttendance that we had identified in the previous chapter. We will also learn how to populate database with data, manipulate data in that and retrieve data from the database through SQL queries.
Installing MySQL
MySQL is an open source RDBMS software which can be easily downloaded from the official website https:// dev.mysql.com/downloads. After installing MySQL, start MySQL service. The appearance of mysql> prompt (Figure 8.1) means that MySQL is ready for us to enter SQL statements.
Few rules to follow while writing SQL statements in MySQL:
- SQL is case insensitive. That means name and NAME are same for SQL.
- Always end SQL statements with a semicolon (;).
- To enter multiline SQL statements, we don’t write ‘;’ after the first line. We put enter to continue on next line. The prompt mysql> then changes to ‘->’, indicating that statement is continued to the next line. After the last line, put ‘;’ and press enter.
Constraints are certain types of restrictions on the data values that an attribute can have. They are used to ensure the accuracy and reliability of data. However, it is not mandatory to define constraint for each attribute of a table. Table 8.2 lists various SQL constraints.
CREATE Database To create a database, we use the CREATE DATABASE statement as shown in the following syntax: CREATE DATABASE databasename;
To create a database called StudentAttendance, we will type following command at mysql prompt.
mysql> CREATE DATABASE StudentAttendance;
Query OK, 1 row affected (0.02 sec)
Note: In LINUX environment, names for database and tables are case-sensitive whereas in WINDOWS, there is no such differentiation. However, as a good practice, it is suggested to write database or table name in the same letter cases that were used at the time of their creation. A DBMS can manage multiple databases on one computer. Therefore, we need to select the database that we want to use.
Once the database is selected, we can proceed with creating tables or querying data. Write the following SQL statement for using the database:
mysql> USE StudentAttendance;
Database changed Initially,
the created database is empty. It can be checked by using the Show tables command that lists names of all the tables within a database.
mysql> SHOW TABLES;
Empty set (0.06 sec)
CREATE Table
After creating database StudentAttendance, we need to define relations (create tables) in this database and specify attributes for each relation along with data types for each attribute. This is done using the CREATE TABLE statement.
Syntax:
CREATE TABLE tablename( attributename1 datatype constraint, attributename2 datatype constraint, : attributenameN datatype constraint);
- It is important to observe the following points with respect to the Create Table statement:
- N is the degree of the relation, means there are N columns in the table.
- Attribute name specifies the name of the column in the table.
- Datatype specifies the type of data that an attribute can hold.
- Constraint indicates the restrictions imposed on the values of an attribute. By default, each attribute can take NULL values except for the primary key.
Create table STUDENT.
mysql> CREATE TABLE STUDENT(
-> RollNumber INT,
-> SName VARCHAR(20),
-> SDateofBirth DATE,
-> GUID CHAR(12),
-> PRIMARY KEY (RollNumber));
Query OK, 0 rows affected (0.91 sec)
DESCRIBE Table
We can view the structure of an already created table using the describe statement.
Syntax: DESCRIBE tablename; MySQL also supports the short form DESC of DESCRIBE to get description of table. To retrieve details about the structure of relation STUDENT, we can write DESC or DESCRIBE followed by table name:
The show table command will now return the table STUDENT:
mysql> SHOW TABLES;
- Using the Drop statement to remove a database will ultimately remove all the tables within it.
- DROP statement will remove the tables or database created by you. Hence you may apply DROP statement at the end of the chapter.
-> SDateofBirth, GUID)
-> VALUES (1,'Atharv Ahuja','2003-05-15',
-> 444444444444);
Query OK, 1 row affected (0.02 sec)
Continue.....
important search queries
database concepts pdf class 11 database concepts pdf class 12 database concepts class 12 notes what is database class 11 introduction to database concepts pdf dbms class 11 notes database concepts class 11 ncert solutions mcq on database concepts class 11,database concepts pdf class 11 dbms class 11 notes mysql notes for class 11 pdf sql class 11 notes sql class 12 notes pdf database concepts class 11 ncert solutions what is database class 11 questions on database concepts class 11
0 Comments