SQL is known by many people as Standard Query Language. It's a programming language to communicate with a database. It's used by MySQL, PostgreSQL, SQL server, DB2, and Oracle.
Creating and Displaying Databases
The creation of databases is done by using the CREATE DATABASE statement.
A valid database name may contain only letters, numbers, and underscore symbols. The database name must contain at least one alphabet letter and the name sare always stored in small letters.
It's a good practice to name databses with same meaningful characters
The created database can be dsplayed by using the SHOW DATABASE statement,
So the statements that are used are: CREATE DATABASE and SHOW DATABASE.
Selecting and Using Existing Databases.
Users must select the database before creating new tables or retun the data rows from existing tables. Selecting the databse is really impotant because multiple databses may contain similar table names.
The database is selected by using the USE statement.
Example: USE databasename;
*** (SELECT * FROM *)
Removing or deleting Databases
The DROP DATABASE statement is use to tremove or delete the databse checking all the tables and data rows in it. Because forebre deleting the databse because the system does not ask for confirmaton.
Example: DROP databasename;
Creating a database table
The CREATE TABLE statement is used to create a database table.
A valid databasre table name may contain only letters, numebrs and underscore symbol.
The database table name are case insentive.
A single database must contain aunique table name, while in multuple databases, the same table name can created in another database.
Example:
CREATE TABLE tablename (
colummname1 datatype,
colummname2 datatype
);
or
CREATE TABLE students 3(
studentid SMALLINT(5)
firstname VARCHAR(20)
SHOW TABLES: to show tables
DESCRIBE or EXPLAIN to get details of table
Modify or updating the Database table
The ALTERTABLE statment is used to remove, add or drop table columns in the creating of a database table. Th remive the table ALTERTABLE oldtablename RENAME newtablename;
To add a new colum in exsiting table;
ALTERTABLE table name ADD Columnname datatype;
To modify the columndata type in exisiytn table ALTERTABLE tablename MODIFY COLUMN columnname datatype;
To drop a colum fron an existaning table: ALTERTABLE tablebane DROPCOLUMN columname;
Remove or delete table
You can create as well as remove database tables from your existing database.
The DROPTABLE statemtn is used to remove or delete the database table incluidnf all datarows.
Becareful beofr deleting the databs table complete from your database because the system does not ask for confirmation.
INSERT INTO statement
The INSERT INTO statement is used to insert or add new data rows in a table
INSERT INTO tablename VALUES (value 1, value 2, value 3);
columnames are not spevifified, values for each colum must be specififed sequencially by olum.
INSERT INTO tablename (colunm1, column2, column3) VALUES (value1,value2,value3);
The String values ist be wrapped in a single quotes.
SELECT * FROM tablename ---> to extract data from table
INSERT INTO SELECT statement
In SQL we can copy the data rows from one table to another table.
Copy from sourcetable and paste to forge a table from both accounted in a single SQL statement
The source table and target tale must have ??? table defintion.
Any exisiting data rows or records in targt table remains ubaffected.
Eample:
INSERT INTO targettable SELECT * FROM sourcetable;
INSERT INTO targettable (colum1, coumn2) SELECT (column1, column2) FROM sourceTable;
-----------------------
Good example
CREATE TABLE students2(
studentid INT PRIMARY KEY AUTO_INCREMENT,
firstname VARCHAR(40) NOT NULL,
lastname VARCHAR(40) NOT NULL,
Class VARCHAR (20),
age, INT
----------------------
DELETE FROM tablename ----> TO delete data from table
UPDATE Statement
UPDATE Statement is used to update the data rows in a table. The UPDATE statement can update one or mu;tiple colum values in a single SQL statement.
WHERE clause is used to specifify the data row to be updated. Any existing data row or record in target table remains unacffected.
Example:
UPDATE tablemap SET Colum1=newvalue;
UPDATE tablename SET column1 = new value, Colum 2=new values
WHERE columnid=3;
UPDATE stdents SET class= "second" WHERE firstname= "John" AND lastname ="Doe";
DELETE STATEMENT
DELETE statement is used to delete the dta rows in table
DELETE statement can delete one or multiple rows from a table
WHERE clouse is used to specificy the data row to be deleted
Examples:
DELETE FROM
DELETE FROM tablename WHERE COLUMN1 = value;