Sie sind auf Seite 1von 2

Show table format with column names and data types Inserting Data:

>DESCRIBE <table name>


A Quick Guide To MySQL >DESCRIBE teachers; > INSERT INTO <table> (<columns>) VALUES (<data>)
> INSERT INTO teachers (id, name, age) VALUES
Tables & Queries Modify the structure of table
>ALTER TABLE <table name> <alter
(NULL, ‘John’, ‘12’);

speciications> Loading Data from Files:


This is a Quick reference Guide for MySQL 5.x. >ALTER TABLE teachers DROP COLUMN salary;
>ALTER TABLE teachers ADD COLUMN salary >LOAD DATA LOCAL INFILE ‘<ilename>’ INTO TABLE
MySQL is a relational database management system INT(5); <table>
(RDBMS) based on SQL (Structured Query Language). >ALTER TABLE teachers CHANGE irstName >LOAD DATA LOCAL INFILE ‘ile.sql’ INTO
MySQL is available under GNU public license and name VARCHAR(20); TABLE teachers;
through subscription to MySQL Network for business Delete the table This is very convenient to load data directly from iles when
applications. It runs on Unix, iMac, and Windows and >DROP TABLE <table name> you have thousands of entries.
>DROP TABLE teachers;
provides rich API for many programming languages
Modifying Data:
including C, C++, Java, Perl, Python, Ruby and PHP. Retrieving Data:
>UPDATE <table> SET <ield1> = <value1> AND <ield2>
Database Queries: >SELECT <columns> FROM <tables> WHERE <conditions> = <value2> WHERE <conditions>
>UPDATE teachers SET status = ‘enrolled’
List all databases WHERE fees = ‘paid’;
Retrieve from all columns
>SHOW databases; >SELECT * FROM <tables> Deleting Data:
>SELECT * FROM teachers;
Select the database
>USE <database name> >DELETE FROM <table> WHERE <condition>
Retrieve from selected columns >DELETE FROM teachers WHERE fees = ‘paid’;
>USE university;
>SELECT <column 1>, <column 2> FROM <tables>
Create a database >SELECT id, name FROM teachers; Pattern Matching Examples:
>CREATE DATABASE <database name>
>CREATE DATABASE university; Retrieve from selected tables >SELECT * FROM teachers WHERE name LIKE
‘j%’;
>SELECT <columns> FROM <table 1>, <table 2>
Delete a database Wildcard % selects joe, john, jones, etc.
>SELECT teachers.name, students.name FROM
>DROP DATABASE <database name> teachers, students;
>DROP DATABASE university; > SELECT * FROM teachers WHERE name LIKE
‘ _ _ _ ’;
Retrieve unique values Selects 3 character values.
Rename a database
>SELECT DISTINCT <column name> FROM <table>
>ALTER DATABASE <database name> RENAME <new
database name> >SELECT DISTINCT name FROM teachers; > SELECT * FROM teachers WHERE name
>ALTER DATABASE university RENAME faculty REGEXP ‘^A’;
Selects all entries beginning with A.
Retrieve data satisfying a given condition
Table Queries: >SELECT <columns> FROM <tables> WHERE <condition>
> SELECT * FROM teachers WHERE name
>SELECT name FROM teachers WHERE age > 35; REGEXP ‘p$’;
Create a table
>CREATE TABLE <table name> (<ield name> <ield type> Selects all entries ending with p.
Retrieve data satisfying multiple conditions
(<ield size>), …)
>SELECT <columns> FROM <tables> WHERE <condition> [abc] match a, b, or c
>CREATE TABLE teachers (name varchar(20),
age INT(10)); AND <condition> [^abc] match all expect a, b, or c
>SELECT name FROM teachers WHERE age > 35 [A-Z] match uppercase
List all tables in the database AND gender = ‘female’; [a-z] match lowercase
>SHOW tables; [0-9] match any digit
* match zero or more instances
+ match one or more instances
? match zero or one instance
. match any single char
^ match the beginning
$ match the end
| separates alternatives

{n,m} match at least n times but notmore


than m times

{n} string must occur exactly n times


{n,} string must occur at least n times

Sorting:

>SELECT <columns> FROM <table> ORDER BY


<column> <ASC or DESC>
>SELECT * FROM teachers ORDER BY age;
>SELECT * FROM teachers ORDER BY name
DESC;

Sorts the query results. This document was written by Awais Naseem & Nazim
Rahman from EMBnet Pakistan Node and being distributed
Limiting: by Education & Training Program Committee of EMBnet.

>SELECT <columns> FROM <table> LIMIT <from>, <to> EMBnet – European Molecular Biology Network – is a
>SELECT * FROM teachers LIMIT 1,5; bioinformatics support network of bioinformatics support
centers situated primarily in Europe.
Limits query results to speciic range.

Grouping: http://www.embnet.org/

>SELECT <columns> FROM <table> GROUP BY <column> A Quick Guide To MySQL Tables & Queries
>SELECT name, COUNT(*) FROM faculty GROUP First edition © 2010
BY name;

GROUP BY is used to group values from a column. You can


also perform calculations on that column using count, sum,
avg. Count returns the number of rows which are selected.

Das könnte Ihnen auch gefallen