Begin Web Programming with PHP & MySQL

Advertisements



SQL Commands

SQL is operated through simple declarative statements. Data is stored in a relational database. We can manage the data using any relational database management systems such as SQL Server and MySQL.

CREATE

The CREATE command can be used for creating MySQL databases and tables.
The basic syntax of this CREATE DATABASE statement is:
CREATE DATABASE DatabaseName;

The basic syntax of this CREATE TABLE statement is:
CREATE TABLE table_name ( column1 data type, column2 data type, ...., column’n’ data type, );

INSERT

The basic syntax of this INSERT statement is:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.
INSERT INTO table_name VALUES (value1, value2, value3, ...);

UPDATE

The basic syntax of this UPDATE statement is:
UPDATE table-name SET field1 = value1, field2 = value2,... [WHERE condition]

  • We can update one or more fields at a time.
  • We can specify any condition using the WHERE clause.
  • We can update the values in a single table at a time.
Update Table structure using ‘ALTER’ command

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
To add a column in a table, use the following syntax:
ALTER TABLE table-name ADD column-name datatype;

To delete a column in a table, use the following syntax:
ALTER TABLE table-name DROP COLUMN column-name;

To modify the data type of a column in a table, use the following syntax:
ALTER TABLE table-name MODIFY COLUMN column-name datatype;

DELETE

DELETE FROM table-name [WHERE condition]

  • If the WHERE clause is not specified, then all the records will be deleted from the given table.
  • We can delete records in a single table at a time.

Delete Tables and Databases using 'DROP' command
DROP DATABASE database-name; The DROP DATABASE statement is used to drop an existing MySQL database.

DROP TABLE table-name;
The DROP TABLE statement is used to drop an existing table in a database.

SELECT

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

1 )SELECT column1, column2, …, column n FROM table-name;
Values from the selected columns of the tables are returned.

2)SELECT column1, column2, …, column n FROM table-name WHERE condition(s);
We can define conditions based on which the data is selected from the table.

3)SELECT * FROM table-name;
All the data from the table is selected. You can use WHERE condition too.

4)SELECT DISTINCT column1, column2, …, column n FROM table-name;
The SELECT DISTINCT statement is used to return different values (without repetition).

5)SELECT COUNT(*) FROM table-name;
It returns the number of rows in the given table.