Showing posts with label MySQL. Show all posts

Search Query with SQL

  No comments
09:18


The search system is a system used to search for data that has been stored previously in the database, for a little data may not be necessary, but as more search data is very important to find the data we want, to save time and exact destination / data sought According to what we need, For example: Google, who does not know google? This biggest search engine is one such example, Here are some examples of queries to search:Example 1:
    SELECT table1.field1 FROM table1
    WHERE table1.field1 LIKE "% search%"
    ORDER BY table1.field1 DESC LIMIT 20

In the query "SELECT table1.field FROM table1" so that is shown only field1 in table1 of table1 only to display all the filed can be replaced tabel1.fieldnya with *, then WHERE table1.field1 LIKE "% search%" so with filed1 conditions in table1 We search on the search according to the word we write by replacing the word "search" with the word we want to search, ORDER BY table1.field1 DESC LIMIT 20 sorted by filed1 in table1 by descending (az) with limit displayed / limited by 20 data.But with the query sometimes we use like "% keyword%" is in SQL Statement. What we look for does not appear the example of the scenario, we want to find the top 20 who have the name "eka" because there will be a lot of names, and not to mention the name of the first name or bleakangnya name such as eka haryanto, haryanto eka etc, Finally searched eka does not appear in the top 20 list / top, then the solution using the query model as follows.Example 2:

    SELECT tbl_a.username FROM tbl_a
    WHERE tbl_a._ama_anggota LIKE "% eka%"
    ORDER BY case
    When tbl_a.nama_anggota like "eka" then 1
    When tbl_a.nama_anggota like "eka%" then 2
    When tbl_a.nama_anggota like "% eka" then 3
    Else CONCAT (4, tbl_a.name_members) end LIMIT 20

In the query "SELECT a.name_member FROM tbl_a" so that is displayed only the name of the member in table A of table A only, then WHERE tbl_a.nama_anggota LIKE "% eka%" so with condition nama_anggota in table A that we search on search according to word We write with the word "eka" the word we want to search, then ORDER BY case when tbl_a.nama_anggota like "eka" then 1 when tbl_a.nama_anggota like "eka%" then 2 when tbl_a.nama_anggota like "% eka" then 3 else CONCAT (4, tbl_a.name_members) end LIMIT 20 hereby forces the name of the member whose last name appears at the top followed by the rear variant, the front and the remaining variants, with the limit displayed / limited by 20 data.Thus Query search with SQL there are 2 case examples, where example 1 is a common case example, whereas example2 is a special case example,If any suggestions / maybe there is another statement better please free berargumen but with good manners ya, Happy Coding, And Fighting :)

Read More

Commands on Databases and Tables with MYSQL

  No comments
14:39


MySQL is a database management system software (DBS) or multithread, multi-user DBMS, with around 6 million installations worldwide. MySQL AB makes MySQL available as free software under the GNU General Public License (GPL) license, but they also sell under a commercial license for cases where its use does not match the use of the GPL.


Unlike projects like Apache, where software is developed by the general community, and copyright to source code is owned by their respective authors, MySQL is owned and sponsored by a Swedish commercial company MySQL AB, which holds the copyright almost over All the source code. The two Swedes and one Finn who founded MySQL AB were: David Axmark, Allan Larsson, and Michael "Monty" Widenius. [Wikipedia]



Here's a collection of Commands on MySQL


1. On the Database


A. Create a database

To create a new database, so it does not apply if the database already exists or you have no privileges.

The syntax:

CREATE DATABASE nama_db


B. Delete the database

To delete the database and all the tables inside it. This command does not apply if the database does not exist or you have no privileges. The syntax:

DROP DATABASE nama_db


C. Using a database

To make the database the default and reference of the table you will use later. This command does not apply if the database does not exist or you have no privileges. The syntax:

USE nama_db


D. Displays the database

To display the list in the current system. The syntax:

SHOW DATABASES



    The view is:

+ ----------------- +

| Database |

+ ----------------- +

| Contoh_db |

| Mysql |

| Test |

| Exam |

+ ----------------- +

4 rows in set (0.00 sec)



2. In Table


A. Create table

To create a table at least you need to specify the name and type of column you want. The simplest syntax (without any other definition) is:

CREATE TABLE nama_tbl

(Column1 tipekolom1 (), columnek tipekolom2 (), ...)

Example: You want to create a table with a profile name that has a name field (char type, width 20), age column (integer type), sexy type column (enum type, contains M and F). The syntax:

        CREATE TABLE profile (

Name of CHAR (20), age INT NOT NULL,

ENUM type ('F', 'M'));


While a rather complete command in creating a table is to include a particular definition. For example a command like this:

        CREATE TABLE participants (

          No SMALL INT UNSIGNED NOT NULL AUTO_INCREMENT,

          CHAR Name (30) NOT NULL,

          Fields ENUM ('TS', 'WD') NOT NULL,

          PRIMARY KEY (No),

          INDEX (Name, Field of Study));


The above command means to create a table of participants with the column No. as PRIMARY KEY is a unique table index that can not be duplicated with the AUTO_INCREMENT attribute is a column that can automatically sort the numbers filled in to it. While the Name and Field column is used as a regular index.


B. Create an index in the table

Adding an index to an existing table is either unique or common.


The syntax:

CREATE INDEX nama_index ON nama_tbl (nama_kolom)

    CREATE UNIQUE INDEX nama_index ON nama_tbl (nama_kolom)



C. Delete table

To delete a table in a particular database. If done then all contents, indexes and other attributes will be erased. The syntax:

    DROP TABLE nama_tbl


D. Delete index

To delete an index on a table. The syntax:

    DROP INDEX name-index ON nama_tbl


E. View table information

To see what tables exist in a particular database. The syntax:

SHOW TABLES FROM nama_db

As for viewing the table description or information about the column use the syntax:

DESC nama_tbl nama_kolom

    Or SHOW COLUMNS FROM nama_tbl FROM nama_db

 

Example for example above will be displayed:

+ ----------------------------- +

| Tables_in_contoh_db |

+ ----------------------------- +

| Participants |

| Profile |

+ ----------------------------- +

2 rows in set (0.00 sec)

+ ------------------------- + ----------------------- - + ------- + -------- + --------------- + ------ +

| Field | Type | Null | Key | Default | Extra |

+ ------------------------- + ----------------------- - + ------- + -------- + --------------- + ------ +

| Name | Char (20) | YES | | NULL | |

| Age | Int (11) | | 0 | | |

| Jenis_kelamin | Enum ('F', 'M') | YES | | NULL | |

+ ------------------------- + ----------------------- - + ------- + -------- + --------------- + ------ +

3 rows in set (0.02 sec)


F. Obtain or display information from the table

To display the contents of the table with certain options. For example to display the entire contents of the table is used:

SELECT * FROM nama_tbl

    To display only certain columns:

SELECT column1, column2, ... FROM nama_tbl

    To display the contents of a column with certain conditions

SELECT column1 FROM name_tbl WHERE column2 = isikolom

G. Modify table structure

Can be used to rename a table or change its structure such as adding columns or indexes, deleting columns or indexes, changing column types etc. Common syntax:

    ALTER TABLE nama_tbl action

    To add a new column in a specific place you can use:

        ALTER TABLE nama_tbl

            ADD column_new type () definition

To add a new_name of integer type after column1 is used:

    ALTER TABLE nama_tbl

ADD column_new INT NOT NULL AFTER column1


To add a new index to a particular table both unique and ordinary:

   ALTER TABLE nama_tbl ADD INDEX nama_index (nama_kolom)

   ALTER TABLE nama_tbl ADD UNIQUE name_index (nama_kolom)

   ALTER TABLE nama_tbl ADD PRIMARY KEY nama_indeks (nama_kolom)


To change the column names and their definitions, for example rename new_name with integer type to new_kolom with char type with width 30 used:

        ALTER TABLE nama_tbl

        CHANGE column_new new_kolom CHAR (30) NOT NULL

To delete a column and all its attributes, for example deleting column1:

    ALTER TABLE nama_tbl DROP column1


To remove indices either unique or commonly used:

    ALTER TABLE nama_tbl DROP nama_index

    ALTER TABLE nama_tbl DROP PRIMARY KEY


H. Modify the information in the table.

To add a new record or row in the table, the syntax is:

    INSERT INTO nama_tbl (nama_kolom) VALUES (isi_kolom)

Or INSERT INTO nama_tbl SET nama_kolom = 'isi_kolom'


For example to add two rows in the profile table with the content name = deden & ujang and content age = 17 & 18 are:

    INSERT INTO profile (name, age) VALUES (deden, 17), (ujang, 18)

Or INSERT INTO profile SET name = 'deden', age = '17 ';

    INSERT INTO profile SET name = 'ujang', age = '18 ';


To modify an existing record or row corresponding to a column. For example to change the deden life to 18 in the example above can be used syntax:

        UPDATE profile SET age = 18 WHERE name = 'deden';


To delete a particular record or row in a table. For example to delete the existing row of the named name used syntax:

        DELETE FROM profile WHERE name = 'ujang';

    If WHERE is not included then all contents in the profile table will be deleted.


So a collection of database commands and tables in MySQL, sorry if there are errors, hopefully useful.

Read More

Data Structure In MySQL

  No comments
14:17


In computer science terms, a data structure is a way of storing, arranging and organizing data in computer storage media so that data can be used efficiently.
In programming techniques, data structures mean data layouts that contain columns of data, be they columns visible to users or columns only used for programming purposes that are not visible to the user. Each row of the set of columns is called a record. The width of the columns for data may vary and vary. There are columns whose width changes dynamically according to user input, and there are also fixed width columns. By this nature, a data structure can be applied to database processing (for example for financial data purposes) or for word processors whose columns change dynamically. Examples of data structures can be seen in spreadsheet files, data base (database), word processing, compressed imagery, as well as file compression with certain techniques that utilize data structures. [Wikipedia]



The structure of data storage on mysql is as follows.
                                                          Database

                                                               / \

                                                      Table      Table

                                                           /  \         / \

                                                       Field Field     Field Field

    The default database that belongs to root is mysql and test.
    To be able to see the database contained in mysql used commands

        mysql> show databases;

        Then it will be visible

           + ------------------------- +

            | Database |

           + ------------------------- +

            | mysql |

            | Test |

           + ------------------------- +

     To be able to use mysql database, use

          mysql> use mysql;



      To view the contents of the mysql database (the table contained in the mysql database)

          mysql> show tables;

(Note: If you have not selected the database used, then the command becomes

 mysql> show tables from mysql; )

          Then will be seen:





      To view the contents of the user table (fields contained in the user table)

         mysql> show fields from user;

        Then it will look:





  To view the contents of the data contained in the host field, user, password is used

    mysql> select host, user, password from user;

  To see all the data contained in the user field is used

    mysql> select * from user;

Root can add a new user on the mysql server

   Suppose root adds a user with a custom password on the host localhost.

   mysql> insert into user values ​​("localhost", "budi", password ("adi"), "Y", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "Y", "N", "Y", "Y", "Y");

As root, you should always keep an eye on the data security issues in your server database, one of the ways to prevent accessing the database by others and using your database permissions is by giving passwords to users. Passwords stored in the user table should be stored in the form of encryption, therefore commands are used

                    Password ("adi")

In the above syntax, for users who do not have a password, the user should be removed, the syntax:

    mysql> delete from user where password = '';

Root can change its password in a way

    mysql> update user set password = password ("password_new") where user = "root";

For more details, see section E.


So a brief explanation of the data structure, apologize if there is a mistake, hopefully can be understood,

Read More

How to Login to MySQL Server

  No comments
13:55



MySQL server can be run on both Windows operating system and UNIX family. The query syntax on Win 9x and UNIX family is the same. Syntax query in MySQL is not case sensitive.




As for how to login to Mysql server is as follows:


1. in Win 9x


Open command prompt mode (run -> command), assume MySQL is installed in MySQL directory, then user must move to MySQL directory then go to bin directory.


Assuming, the initial location in the command prompt window is drive C, the user name is mature and the password is ok


Syntax: (note: number to indicate step sequence)


 C:\cd mysql
 C:\mysql> cd bin
 C:\mysql\bin>inmysqladmin
 C:\mysql\bin>mysql -u budi -p
     Password: ***


If successful there will be a prompt


 Welcome to the MySQL monitor. Command end with; tr \ g.


 Your MySQL connection id is 14 to server version: 3.23.49


 Type 'help:' or '\ h' for help. Type '\ c' to clear the buffer.


2. In the UNIX Family


In the shell type


#Mysql -u budi -p

   Password: ***


If successful there will be a prompt


 Welcome to the MySQL monitor. Command end with; Tr \ g.


 Your MySQL connection id is 14 to server version: 3.23.49


 Type 'help:' or '\ h' for help. Type '\ c' to clear the buffer.



So how to get into the MySQL server that can be delivered hopefully useful.

Read More