Basic MySQL

Basic MySQL


Create or Drop a Database

Starting with something simple, you can use the CREATE command to create a new database. The syntax is

CREATE DATABASE IF NOT EXISTS [yourDBName];

When you create a database with this command, you're really just creating a directory to hold the files that make up the tables in the database.

To delete an entire database from the system, use the DROP command:

DROP DATABASE IF EXISTS [yourDBName];

Be extremely careful when using the DROP command,
because once you drop the database,


You can also use the CREATE command to create a table within the current database. The syntax is

CREATE TABLE [yourTableName] ([fieldName1] (type], [fieldName2]
[type], ...) [options]

To delete a table from the current database, use the DROP command:

DROP TABLE [yourTableName];

Be extremely careful when using the DROP command, because once you drop the tables,

Altering a Table


To add a column to a table, use this:

ALTER TABLE [yourTableName] ADD [newColumn] [fieldDefinition];

To delete a column from a table, use this:

ALTER TABLE [yourTableName] DROP [columnName];

To change a column from one type to another, use this:

ALTER TABLE [yourTableName] CHANGE [columnName]
[newfieldDefinition];

To make a unique column in your table, use this:

ALTER TABLE [yourTableName] ADD UNIQUE [columnName]
([columnName]);

To index a column in your table, use this:

ALTER TABLE [yourTableName] ADD INDEX [columnName]
([columnName]);

Using the ALTER command alleviates the need to delete an entire
table and re-create it .

Insert, Update, or Replace tables record


The INSERT and REPLACE commands populate your tables one record at a time. The syntax of INSERT is

INSERT INTO [yourTableName] ([fieldName1], [fieldName2], ...)
VALUES ('[value of fieldName1]', '[value of fieldName2]'...);

When inserting records, be sure to separate your strings with single quotes or double quotes. If you use single quotes around your strings and the data you are adding contains apostrophes, avoid errors by escaping the apostrophe (\ ') within the INSERT statement. Similarly, if you use double quotes around your strings and you want to include double quotes as part of the data, escape them (\ ") within your INSERT statement.

The REPLACE command has the same syntax and requirements as the INSERT command. The only difference is that you use REPLACE to overwrite a record in a table, based on a unique value:

REPLACE INTO [yourTableName] ([fieldName1], [fieldName2], ...)
VALUES ('[value of fieldName1]', '[value of fieldName2]'...);

The UPDATE command modifies parts of a record without replacing the entire record. To update an entire column in a table with the same new value, use this:

UPDATE [yourTableName] SET [fieldName] = '[new value]';

If you want to update only specific rows, use a WHERE clause:

UPDATE [yourTableName] SET [fieldName] = '[new value]' WHERE [some
expression];

UPDATE can be a very powerful SQL command.


Deleting from a Table


Like the DROP command, using DELETE without paying attention to what you're doing can have horrible consequences in a production environment. Once you drop a table or delete a record, it's gone forever. Don't be afraid; just be careful. To delete all the contents of a table, use the following:

DELETE FROM [yourTableName];

If you want to delete only specific rows, use a WHERE clause:

DELETE FROM [yourTableName] WHERE [some expression];

If you're going to start deleting records, be sure you have a backup.



Selecting from a Table

When creating database-driven Web sites, the SELECT command will likely be the most often-used command in your arsenal. The SELECT command causes certain records in your table to be chosen, based on criteria that you define. Here is the basic syntax of SELECT:

SELECT [field names] FROM [table name]
WHERE [some expression]
ORDER BY [field names];

To select all the records in a table, use this:

SELECT * FROM [yourTableName];

To select just the entries in a given column of a table, use this:

SELECT [columnName] FROM [yourTableName];

To select all the records in a table and have them returned in a particular order, use an expression for ORDER BY. For example, if you have a date field for record entries and you want to see all the record entries ordered by newest to oldest, use this:

SELECT * FROM [yourTableName] ORDER BY [dateField] DESC;

DESC stands for "descending." To view from oldest to newest, use ASC for "ascending." ASC is the default order.

You can also perform mathematical and string functions within SQL statements,


PHP Functions
Php tutorial - imagemagick
php.ini Basics
PHP Sessions
Cookies Versus Sessions
PHP Web-Related Variables
PHP ERRORS
maximum size of a file uploaded
Php Image upload
php file_get_contents
MySQL Data on the Web
What are GET and POST
php and pdf
$_ENV and $_SERVER
PEAR with php
SELECTING DATA PHP
prevent hijacking with PHP
LAMP
PHP MySQL Functions
PHP Zip File Functions
Substrings PHP
PHP Variable names
PHP magic methods
How to get current session id
Add variables into a session
$_GET , $_POST,$_COOKIE
different tables present in mysql
PHP CURL
php Sessions page
PHP-sorting an array
PHP-count the elements of array
Operators for If/Else Statements
PHP file uploading code
PHP global variables
Testing working using phpinfo
PHP Code for a Valid Number
PHP-Associative Arrays
PHP mvc tutorial
PHP get_meta_tags-Extracts
difference between print and echo
PHP best tutorial-PHP variables
Reading DOC file in PHP
PHP interview questions
convert time PHP
PHP implode array elements
header function-PHP
PHP-Renaming Files Directories
PHP Classes
in_array function in PHP
keep your session secure PHP
Web Application with PHP
What is SQL Injection
PHP-extract part of a string
PHP urlencode
PHP- know browser properties
PHP- Extracting Substrings
Checking Variable Values /Types
PHP-best 20 Open Source cms
IP AddressPHP
PHP-Scope Resolution Operator
how create new instance of object
how eliminate an object
PHP- ob_start
XML file using the DOM API
PHP- MVC
PHP- CAPTCHA
PHP- Position of a Value in an Array
PHP-Mail Functions
PHP-difference include vs require
calculate the sum of values in an array
PHP-total number of rows
Show unique records mysql
MySQL Triggers
MySQL data directory
MySQL Subqueries
PHP- Networking Functions
PHP- Operators
Restore database
Conditional Functions mysql
PHP-function overloading
Friend function
mysql_connect /mysql_pconnect
PHP-Error Control Operators
what is IMAP
Apache-Specific Functions
Send Email from a PHP Script
SQL inherently
WAMP, MAMP, LAMP
Php tutorial-SYMBOLS
Table Types-MySQL
PHP-Encryption data management
PHP Array
Running MySQL on Windows
Maximum Performance MySQL
XML-RPC
PHP-static variables
Advanced Database Techniques
FTP
Codeigniter
Apache Pool Size
Why NoSQL
MySQL Server Performance
Database software
SQL Interview Answers
PHP Redirect
PHP Interview Questions with Answers
Advanced PHP
Related Posts:
  • Relational Databases-MySQL MySQL is a relational database. An important feature of relational systems is that a single database can be spread across several tables as opposed to our flat-file phone book example. Related data is stored in separat… Read More
  • Backing Up and Restoring Data MySQL Even the best maintained databases occasionally develop problems. Hardware failures, in particular, can really throw a monkey wrench into your web pages. Now that you're using a database, just backing up the files (HTM… Read More
  • Advanced SQL Many of the features shown in this chapter are specific to MySQL's version of SQL. For example, MySQL's functions are useful tools for working with strings, dates and times, and math. Also, we'll show some w… Read More
  • Managing the Database Creating Users To create users above and beyond the default privileged root user, issue the grant command. The grant command uses this syntax: GRANT PRIVILEGES ON DATABASE.OBJECTS TO'USER'@'HOST' IDENTIFIED BY 'PASSWORD… Read More
  • LOAD DATA INFILE statement MySQL LOAD DATA INFILE provides an alternative to INSERT for adding new records to a table. With INSERT, you specify data values directly in the INSERT statement. LOAD DATA INFILE reads the values from a separate datafile.The… Read More
  • Table Types-MySQL MyISAM is the default table type in MySQL Version 3.23. It's based on the ISAM code andhas a lot of useful extensions. The index is stored in a le with the .MYI (MYIndex) extension, and the data is storedin a le with the .… Read More
  • why need For a relational database? A database or database management system that stores information in tables—rows and columns of data—and conducts searches by using data in specified columns of one table to find additional data in another table. In a rela… Read More
  • Create the months table With MySQL Create the months table as follows: CREATE TABLE months ( month_id INT NOT NULL AUTO_INCREMENT, month VARCHAR (20), days INT, PRIMARY KEY (month_id));      To add the months to the new table, s… Read More
  • Database Backups using mysqldump The MySQL server, and mysql, the MySQL client, a MySQL installation comes with many useful utility programs. We have seen mysqladmin, which is responsible for the control and retrieval of information about an operati… Read More
  • Basic MySQL Basic MySQL Create or Drop a Database Starting with something simple, you can use the CREATE command to create a new database. The syntax is CREATE DATABASE IF NOT EXISTS [yourDBName]; When you create a database wit… Read More
  • mysql_query-executes query mysql_query function  executes query on the default database, set using mysql_select_db() or by a previous query using mysql_db_query(), on the MySQL server connection referenced by connection . If no connection … Read More
  • PHP-Database-Basics-DB-Arrays Adding MySQL to PHP and combining the applications for your dynamic web site is a great start. But, it helps tremendously to structure your database right. We'll give you a solid understanding of both database de… Read More
  • Running MySQL on Windows MySQL supports TCP/IP on all Windows platforms and named pipes on NT. The defaultis to use named pipes for local connections on NT and TCP/IP for all other cases if theclient has TCP/IP installed. The host name speci es whic… Read More
  • Advantages of Using PHP with MySQL Advantages of Using PHP with MySQL   There are several factors that make using PHP and MySQL together a natural choice: PHP and MySQL work well together PHP and MySQL have been developed with each other in … Read More
  • Why MySQL Database? MySQL has its own client interface, allowing you to move data around and change database configuration. Note that you must use a password to log in. Assigning database users allows you to limit access to server tables … Read More