Queries

A query is a question or a request.

We can query a database for specific information and have a recordset returned.

Look at the following query (using standard SQL):

SELECT LastName FROM Employees

Show how to create DB in SQL?

Create a Database

The CREATE DATABASE statement is used to create a database table in MySQL.

We must add the CREATE DATABASE statement to the mysqli_query() function to execute the command.

The following example creates a database named "my_db":

<?php
$con=mysqli_connect("example.com","peter","abc123");

// Check connection
if (mysqli_connect_errno())
{ echo "Failed to connect to MySQL: ". mysqli_connect_error(); }

// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{ echo "Database my_db created successfully"; }
else { echo "Error creating database: ". mysqli_error($con); }?>

Show how to create Table in SQL?

The SQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a table in a database.

Tables are organized into rows and columns; and each table must have a name.

SQL CREATE TABLE Syntax

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

The column_name parameters specify the names of the columns of the table.

The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).

The size parameter specifies the maximum length of the column of the table.

Show how to add data to tables?

SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form does not specify the column names where the data will be inserted, only their values:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Show how to retrieve data from SQL Tables?

The general form of the SELECT statement appears below:

SELECT select_list
FROM source
WHERE condition(s)
GROUP BY expression
HAVING condition
ORDER BY expression

Show how to update data in DB using SQL Queries?

The SQL UPDATE Statement

The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

UPDATE Customers

SET ContactName='Alfred Schmidt', City='Hamburg'

WHERE CustomerName='Alfreds Futterkiste';


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: