SQL Database Management Basics
SQL Database Management Basics
informationn.¶
CREATE DATABASEit is used to create a new empty database.
DROP DATABASEit is used to completely delete a database
existing.
CREATE TABLEis used to create a new table, where the information is
stores really.
ALTER TABLEit is used to modify an existing table.
DROP TABLE is used to completely remove an existing table.
A simple example.
CREATE DATABASE mydb;
USE mydb;
+++++++++++++
This tutorial will show you how to manage databases in MySQL, SQL Server, MS Access.
Oracle, Sybase, DB2 and other databases
What is SQL?
SQL is a standard structured language for queries
SQL allows you to access and manage databases
SQL is a standard (ANSI American National Standards Institute)
SQL is a standard but despite being an ANSI (American National Standards) standard
There are different versions of the SQL language.
And in any case, they continue to meet the ANSI standard as these versions support the
majority of commands such as SELECT, UPDATE, DELETE, INSERT, WHERE
What is an RDBMS?
To build a website that displays data from a database, you will need the following:
An RDBMS database program (MS Access, SQL Server, MySQL)
A server-side language such as PHP or ASP
SQL
HTML/CSS
To carry out these examples, download and install SQL SERVER EXPRESS by clickingHERE
SQL syntax
Tables
SQL statements
Many of the actions you need to perform on a database are done with statements.
SQL
The following statement will select all records from the table 'Persons':
Below is an example of the result of the statement to the table called 'People':
This tutorial will teach you about the different statements in SQL
Keep the following in mind: SQL statements are not case sensitive.
lowercase
Some database systems require a semicolon at the end of each SQL statement.
The semicolon is a standard that terminates each SQL statement in database systems.
that enable more than one SQL statement to be executed in the same call to the server
SQL can be divided into two parts: The Data Manipulation Language (DML) and the
Data Definition Language (DDL)
The DML part of SQL for query and update commands are:
The DDL part of SQL, the most important DDL statements in SQL are:
SELECT column_name(s)
FROM table_name
Example 1
SELECT column_name(s)
FROM table_name
Programming functions like these are not part of this tutorial. To learn how to access
data with the function call, await my next ADO and PHP tutorial.
Within a table, some columns may contain duplicate values. This is not a
problem, sometimes you will want to list only the different (distinct) values in the table
The word DISTINCT can be used as a key to return only the values
different
Example:
SQL WHERE
The where clause is used to extract only the records that meet the criterion.
specified
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
SELECT *
FROM Personas
WHERE LastNames = 'Trejo Lemus'
Quotes in text fields
SQL uses single quotes for text values (many database management systems handle
data accept double quotes). For SQL, text values must be enclosed in quotes
simple
SELECT *
FROM Personas
WHERE Name='Lucero'
SELECT *
FROM Personas
WHERE Name=Lucero
SELECT *
FROM Personas
WHERE P_id = 9
SELECT *
FROM Personas
WHERE P_id = '9'
The AND and OR operators are used to filter records based on more than one condition.
AND operator
The AND operator shows the record if the first condition and the second condition are
true
The OR operator displays the record if the first or the second condition is true.
To select only the people with the first name equal to Marcel Abisag and the last name equal to
Sobrevilla Trejo,
OR Operator
Now we will select the people with the Name field equal to 'Martha' or the Name field
equal to "Elvira"
You can combine AND and OR (using parentheses to form complex expressions)
Now we will select only the people with the Last Name field equal to 'Sobrevilla Trejo' AND
Name equal to 'Marcel Abisag' OR equal to 'Jose Abraham'
SQL ORDER BY
If you want to sort the records in descending order, use the word DESC
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC DESC
Now we are going to select all the people from the table, but showing them in order by the
Name field
ORDER BY DESC
Now we are going to select all the people from the table but showing them in an order.
descending by the field Name with the word DESC
The INSERT INTO statement is used to insert a record or row into a table.
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways.
The first form does not specify the names of the columns where the data will be inserted.
only the values:
The second form specifies the names of the columns and the values inserted.
We would show the result with the statement SELECT * FROM Personas and it would be as follows:
UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value
Note: The WHERE clause in the UPDATE syntax specifies which records will be affected.
updated. If you omit the WHERE clause, all records will be updated.
Now we are going to update the person 'Antonio Trejo Campos' in the People table
We will use the following SQL statements:
UPDATE Personas
Canoga Park
WHERE Name='Antonio' AND LastName='Trejo Campos'
If you omit the WHERE clause, all records will be updated in this way:
UPDATE Personas
Canoga Park
Note: The WHERE clause in the DELETE syntax specifies the record or records that will be
deleted, if you omit the WHERE clause, all records will be deleted from the table
Now we are going to delete the person 'Marco Antonio Trejo Lemus' from the Persons table with the
next statement:
It is possible to delete all the rows in a table without deleting the table itself. This means that the
the structure of the table, attributes and indexes will remain intact:
or
Note: You must be careful when deleting records. Since you will not be able to undo what you do.
with this sentence.
APPENDIX 1
The following code will create the database on the SQL EXPRESS server.
1.- Click on Start --> All Programs --> Microsoft SQL SERVER 2008 --> SQL
Server Management Studio
USE master
if exists (select * from sysdatabases where name='company')
begin
raiserror('The database exists; deleting it....',0,1)
DROP database empresa
end
GO
raiserror('Creating database company....',0,1)
go
CREATE DATABASE company
GO
USE company
GO
CREATE TABLE Personas(
P_id int PRIMARY KEY IDENTITY,
Name nchar(20) NOT NULL,
Last names nchar(30) NOT NULL,
Address nchar(40) NOT NULL,
City nchar(10) NOT NULL
GO
GO
INSERT INTO Personas VALUES ('Marco Antonio', 'Trejo Lemus', 'Calle E 822', 'Tampico')
INSERT INTO Personas VALUES ('Martha Beatriz','Trejo Lemus','Calle E 822','Tampico')
Juana Elvira
INSERT INTO Persons VALUES ('Nora Zulma', 'Trejo Lemus', 'Calle E 822', 'Tampico')
INSERT INTO People VALUES ('Laura Lucero', 'Sobrevilla Trejo', 'Calle E 822', 'Tampico')
INSERT INTO Persons VALUES ('Maria de la Luz', 'Trejo Campos', 'Calle E 822', 'Tampico')
Trinidad
INSERT INTO People VALUES ('Marcel Abisag','Sobrevilla Trejo','E Street
822', 'Tampico') INSERT INTO Personas VALUES ('Jose Abraham', 'Sobrevilla Trejo', 'Calle E
822','Tampico')INSERT INTO People VALUES ('Samuel Salomon','Olmeda Trejo','E Street
Tampico
GO
select * from Persons
quit
5.- Execute the SQL code by clicking on the !Execute option that is displayed.
continuation:
Publishof forMarco Antonio Trejo Lemusin19:188 comments:
The TOP clause is used to specify the number of records that exist.
Can you verify the length of the tables with thousands of records, returning the number of
records
Now we will select only the first two records from the table shown below:
The LIKE operator is used in a WHERE clause to search for a pattern in a column.
LIKE syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
We are going to look for the people who live in the city of Tampico that start with 'Ta' of the
table in question
The sign '%' can be used to define wildcards (letters that are missing in the pattern of
search) both before or after the search pattern
Now we will select the people who live in the city that contains the pattern 'tam'.
the table of people
It is also possible to select the people who live in the city that do not contain the pattern.
tamp from the table persons, using the keyword NOT
SQL Wildcards
Now we will look for the people who live in the city that contains the pattern 'ico' from the table.
People
Now we will select the people whose first name starts with any character.
followed by "Ma" from the table People