Вы находитесь на странице: 1из 3

MYSQL COMMAND REFERENCE (28-07-2008 -14-09-2009)

(BM Shukla)

1.

CREATE TABLE Syntax


CREATE TABLE creates a table with the given name.
A) FOR GENERAL TABLE CREATION
Create Table TableName(fieldname datatype,fieldname1 datatype,)
Example: CREATE TABLE Student (StudentId int(6) not null default 0,Student
varchar(25) not null default , PrimaryKey(StudentId));
B) FOR CREATING TABLE USING AN EXISTING TABLE(ALONG WITH DATA)
Create Table TableName AS Select * from ExistingTableName
Example: CREATE TABLE StudentEmail AS SELECT * FROM PersonEmail;
C) FOR CREATING TABLE USING AN EXISTING TABLE(WITHOUT DATA)
Create Table TableName like ExistingTableName
Example: CREATE TABLE StudentEmail like PersonEmail;
D) FOR CREATING TABLE USING MYSQLDUMP
mysqldump p u username databasename tablename > tablename.sql
mysql -u p username databasename < tablename.sql
Example: mysqldump -p -u sans sanskrit Pustak > pustak.sql
Example: mysql -p -u sans sans sanskrit < pustak.sql

2. ALTER TABLE Syntax


ALTER TABLE enables you to change the structure of an existing table. For example, you can add or
delete columns, change the type of existing columns, or rename columns or the table itself.
A) TO ADD A NEW COLUMN
a) To add the column as the first column of the table.
Alter Table TableName add NewColumnName datatype FIRST
Example: Alter table Student add School varchar(30) not null default FIRST
b) To add the column after a particular column of the table.
Alter Table TableName add NewColumnName datatype AFTER ExistingColumnName
Example: Alter table Student Add School varchar(30) not null default AFTER Student
B) TO DELETE A COLUMN
Alter Table TableName DROP ColumnName
Example: Alter table Student DROP School
C) TO RENAME A COLUMN
Alter Table TableName CHANGE OldColumnName NewColumnName datatype
Example: Alter table Student change School SchoolName varchar(30) not null default ;
D) TO MODIFY A COLUMN DATA-TYPE/SIZE
Alter Table TableName modify ColumnName datatype
Example: Alter table Student modify School varchar(50) not null default ;
E) TO MAKE A COLUMN UNIQUE
a) To make a single column unique.
Alter Table TableName add UNIQUE(fieldname)
Example: Alter table Student add UNIQUE(School);
b) To make multiple columns unique.
Alter Table TableName add UNIQUE(fieldname1, fieldname2, fieldname3)
Example: Alter table Ashtadhyayee add UNIQUE(Bhav, Paad, Sootra);
3. RENAME TABLE Syntax
This statement renames one or more tables.
Rename OldTableName to NewTableName;
Example: Rename Student to StudentDetail
4. DROP TABLE Syntax
This statement will allow deleting a table from the database
DROP table TableName;
Example: DROP table StudentDetail

~1~

MYSQL COMMAND REFERENCE


5.

TO SEE ALL THE TABLES IN THE DATABASE


SHOW tables;

6.

TO SEE DATABASE'S FIELD FORMATS.


Desc tablename;

7.

INSERT Syntax
INSERT inserts new rows into an existing table
A) To Insert data in all columns of a table.
INSERT INTO TableName values ()
Example: Insert into StudentDetail values(1,Ravi,WoodBine),;
B) To Insert data in a column(s) of the table.
INSERT INTO TableName (ColName1, ColName2,..) values(val1,val2,..)
Example: Insert into StudentDetail (StudentId, StudentName) values(2,Shyaam),;
C) To Insert data into a table from another table.
INSERT INTO TableName (ColName1,ColName2) select ColName1,ColName2 From TableName2.
Example: Insert into StudentName (StudentName) select Name from NameLog;

8.

SELECT Syntax
SELECT is used to retrieve rows selected from one or more tables
D) To Select all data from the table
SELECT * FROM TABLENAME;
Example: Select * from StudentDetail;
E) To Select data of a particular Column from the table
SELECT ColumnName FROM TABLENAME;
Example: Select StudentName from StudentDetail;
F) To Select data based on a condition
SELECT * FROM TABLENAME where Condition;
Example: Select StudentName from StudentDetail where StudentId=2;
G) To Select limited number of Records at a time
SELECT * FROM TABLENAME LIMIT no.of records
Example: Select StudentName from StudentDetail LIMIT 10

9.

UPDATE Syntax
UPDATE statement updates columns of existing rows in TableName with new values. The SET clause
indicates which columns to modify and the values they should be given. The WHERE clause, if given,
specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated. A)
To Update data based on a condition
UPDATE TableName SET ColumnName=value where condition;
Example: UPDATE StudentDetail SET StudentName=Rohan where StudentId=1;
B) To Update all records simultaneously
UPDATE TableName SET ColumnName=value where condition
Example: UPDATE StudentDetail SET School=Wendy High School;

10. DELETE Syntax


DELETE statement deletes rows from TableName and returns a count of the number of deleted rows. The
WHERE clause, if given, specifies the conditions that identify which rows to delete. With no WHERE
clause, all rows are deleted.
C) To Delete data based on a condition
Delete fieldname from TableName where condition;
Example: Delete StudentName where StudentId=1;
D) To Delete all records simultaneously
Delete from TableName
Example: Delete * from StudentDetail

~2~

MYSQL COMMAND REFERENCE


11. Create Database Syntax:
Create database <db>
12. Drop Database Syntax:
Drop database <db>
13.1 Add Primary Key:
Alter table <table> add Primary Key <Primary Key Name> (<field1>,<field2>..)
13. 2 Add Unique Key:
Alter table <table> add Unique Key <Unique Key Name>
13.3 Drop Primary Key:
Alter table <table> drop Primary Key <Primary Key Name>
13. 4 Drop Unique Key:
Alter table <table> drop Unique Key <Unique Key Name>
13.5 Add Index:
During Table Creation:
CREATE TABLE Station (
StationId int(6)Primary Key,
StationCode VARCHAR(50,
Station VARCHAR(50),
INDEX (StationCode),
INDEX (Station),
);

{Note: For primary key and Unique Key Indices are created by default}

After Table Creation:


CREATE INDEX StationId ON Station(StationId)
13.6 Drop Index:
ALTER TABLE Station DROP INDEX StationId;

13.7 Show Index:

SHOW INDEX FROM table;

SHOW INDEX FROM table FROM dbname;


SHOW INDEX FROM dbname.table;
14.1 Show Active MySQL Connections:
show processlist;
14.2 Kill an Active MySQL Connections:
kill <id> ;
15. Syntax TO SET THE FIRST CHARACTER IN UPPERCASE AND REST IN LOWERCASE
This command will set the first character of a varchar field in uppercase and the rest in lowercase. Update

TableName set
FieldName=CONCAT(UPPER(SUBSTRING(FieldName,1,1)),LOWER(SUBSTRING(FIELD
NAME,2)));

e.g Update Pincode set


PinCodeLocality=CONCAT(UPPER(PinCodeLocality,1,1)),LOWER(PincodeLocality,2
)));

Вам также может понравиться