Page 177 - trackpad v5.1 class 8 flipbook
P. 177

.....
                     .....
                     column_nameN data_type (size)

                     );
                 While creating a table in SQL, it is necessary to give a data type for each field/column. Let us
                 create a table named Student having StudentId, FirstName, LastName, and Marks  fields in the
                 following way:
                     CREATE TABLE Student
                     (
                     StudentID INT,
                     FirstName CHAR(30),
                     LastName CHAR(30),
                     Marks INT

                     );
                 In the preceding code, you have noticed that a value 30 is given with the CHAR data type within the

                 parenthesis as its size. The meaning of 30 is that you can store a string value upto 30 characters
                 in the FirstName and LastName fields. Whether you store a single character or 30 characters, this
                 field will occupy space required for 30 characters. If you store one character, 29 blank spaces will
                 be inserted with the character.

                 INSERTING RECORDS IN A TABLE

                 After creating a table, you can insert records into it by using the INSERT command. The syntax to
                 insert a record into a table is:

                     INSERT INTO table_name
                     VALUES ('val_column1', 'val_column2', ... , 'val_columnN');
                 Let us insert the following records in the Student table:

                              StudentID            FirstName            LastName               Marks
                                10001                 Amit               Sharma                  450
                                10002                Divya               Kaushik                 480

                                10003               Aadarsh               Kumar                  475
                 To insert above records, we use the following commands:

                     INSERT INTO Student VALUES (10001, 'Amit', 'Sharma', 450);
                     INSERT INTO Student VALUES (10002, 'Divya', 'Kaushik', 480);

                     INSERT INTO Student VALUES (10003, 'Aadarsh', 'Kumar', 475);
                 RETRIEVING RECORDS FROM TABLE

                 The SELECT command retrieves data from one or more tables in a database. It allows you to fetch
                 zero or more rows based on specific criteria. It helps us to join information from different tables
                 and filter specific information as per the required criteria. The SELECT command is the most useful
                 DML command. The ‘SELECT’ statement has many optional clauses which are as follows:



                                                                                               Techipedia (MySQL)  175
   172   173   174   175   176   177   178   179   180   181   182