Getting started with MySQL database

Source: Internet
Author: User
Tags connect mysql mysql commands numeric value php and php and mysql php code variable
mysql| Data | Getting started with database MySQL database
mysql Introduction
What is MySQL?

MySQL is a multi-user, multi-threaded SQL database, is a client/server architecture application, it consists of a server daemon mysqld and a number of different client programs and libraries.
SQL (Structured Query Language Structured Query language) is currently the most widely used and standard database language. SQL language makes it easy to access or update information, and you can query information directly from the Web using SQL. MySQL is fast and flexible enough to meet a website's information management work. Interface programs between the database and the client can communicate using CGI. The new web Chinadns uses PHP, which is combined with PHP and MySQL, and is the best combination for developing data-driven Web sites. PHP is a scripting language for server-side executive interpretation. If you have contacted ASP, you should be more familiar with embedding code in HTML pages. The PHP code is interpreted, executed, and transformed into ordinary HTML page content at one end of the server and sent to one end of the browser. This pattern allows us to use it to perform quite complex functions.


Why use MySQL?
Now everyone's life is almost inseparable from the database, if there is no database, a lot of things will become very difficult, may not be able to do at all. Banks, universities and libraries are some of the places that rely heavily on database systems. On the Internet, use search engines, online shopping and even visit the website address (http://www ...) Can not be separated from the database. A database is typically installed on a computer called a database server. One of the fastest-running SQL (Structured Query Language Structured Query language) databases currently on the market is the MySQL Server, t.c.x by Sweden. Datakonsultab Company Development. MySQL can be downloaded from the http://www.mysql.com/, which provides a rare programming tool for other databases, and MySQL is free for both business and personal users. If you want to use MySQL to develop the application software, must pay a certain amount of product licensing fees, specific circumstances can access the MySQL ' s licensing section.

The features of MySQL are as follows:
Can handle almost unlimited number of users at the same time;
Processing up to 50,000,000 records;
Command execution speed, perhaps the fastest in today;
Simple and efficient user privilege system.


Users of MySQL?

MySQL is widely used in the enterprise, among which well-known companies are:

Silicon Graphics (http://www.sgi.com)

Siemens (http://www.siemens.com)

If that's not enough, you can see more on the MySQL user list.
This article is intended to provide readers with a simple introduction to the basic operation of the MySQL server, including how to establish a connection and set up a server for simple command operations. The commands described here are some of the most important, as they are the base-worn  that make up some advanced commands
MySQL is usually an additional service for the user, so before using, please apply to the new network Chinadns to open a MySQL account.


The MYSQL Environment

MySQL is usually logged in with Telnet (a very good telnet software named Easyterm, which can be downloaded from http://www.arachnoid.com/). We recommend to users the use of Winmysql management tools, download the URL is http://www.tcx.se/Downloads/Win32/myAdmin1.0.1.0.zip through telnet to the Web server after the connection, The second command provides a login for the MySQL server. The steps for logging in are as follows:


1. Connect to the server.


--------------------------------------------------------------------------------

Login:devshed
Password: ********

Last login:wed Aug 09:49:14 from 195.103.124.222
Copyright 1992, 1993, 1994, 1995, 1996 Berkeley Software Design, Inc.
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
The regents of the University of California. All rights reserved.

BSDI Bsd/os 2.1 Kernel #12: Mon Feb EST 1998

You have new mail.
www24:mywww/devshed#


--------------------------------------------------------------------------------

2. Log in to the MySQL account.



--------------------------------------------------------------------------------
www24:mywww/devshed# Mysql-u Devshed-p
--------------------------------------------------------------------------------

Syntax:mysql-h Hostname-u Username-p[password]
Or
Mysql-h hostname-u username--password=password

The user is prompted for a password. -P indicates that the password is entered.


--------------------------------------------------------------------------------
Enter Password: *******
--------------------------------------------------------------------------------

Users will see the following results:


--------------------------------------------------------------------------------
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is-to server Version:3.21.23-beta-log
Type ' help ' for help.

Mysql>
--------------------------------------------------------------------------------

After logging into the database, we can execute a variety of MySQL commands, but we must first call the database before the database operation, that is, to establish a connection to the database:


--------------------------------------------------------------------------------
mysql> use devshed;
--------------------------------------------------------------------------------

Results:


--------------------------------------------------------------------------------
Database changed
Mysql>
--------------------------------------------------------------------------------

Now you have connected to the database. Note that the command is followed by a semicolon (;), and almost all MySQL commands are prefixed with semicolons. The commands for managing the database can be listed by typing Help, \h, or.


--------------------------------------------------------------------------------
Mysql> Help

Help (\h) Display this
Text? (\h) Synonym for ' help '
Clear (\c) Clear command
Connect (\ r) reconnect to the server. Optional arguments are DB and host
Edit \e Edit command with $EDITOR
Exit MySQL (\) exit. Same as quit
Go (\g) Send command to MySQL server
Print (\p) Print current command
Quit (\q) quit MySQL
Rehash (\#) Rebuild completion Hash
Status () Get status information from the server
Use another database with use (\u). Takes database name as argument

Connection id:49 (Can used with mysqladmin kill)

Mysql>
--------------------------------------------------------------------------------

Perhaps none of these features will work, but you should know what each command will do. Like command status, use, print, connect, clear, and quit will be useful when you start to learn. Now you should have some understanding of connecting to databases, selecting databases, and running basic commands. The next step is to explain the basic concepts and methods for operating the database.


MySQL Basics

Data types and Table 
The database is actually composed of different levels of data structure. A structure in MySQL that can hold block (or record) information is a table. These records are made up of smaller information formats, which are data types. One or more data types form a record. A table made up of records forms part of the database. The level of the database can be represented as follows:
Database < Table < record < Datatype
Data types have different forms and sizes, so programmers can build tables based on the needs of actual applications. Choosing the right data type is important to the performance impact of the database, so it is important to understand these concepts in detail.


MySQL Datatypes Data type

MySQL supports a variety of data types (even novice programmers are basically familiar with it). Common types include the following:


char (M) fixed-length character

Used to represent a fixed-length string. The string has a length range of 1-255. For example: Car_model CHAR (10);


VARCHAR (M) variable-length characters
VarChar is a flexible character data type. The string has a length range of 1-255. Choosing varchar is usually a more sensible decision. Although processing char-type data is faster than varchar type data, it can sometimes be 50% faster. (The length of the data stored by the char type is the fixed length when declaring the variable, regardless of the actual length of the data.) The VARCHAR store is based on the actual length of the data, thereby reducing the larger number of data files ) Car_model VARCHAR (10);


int (M) [Unsigned] integer type
int is an integer type, and the value range for storing integers is-2147483648 to 2147483647. You can choose "unsigned" when declaring a type so that the range of values is 0 to 4294967295.
Light_years INT;
Valid integer: '-24567 ',  illegal integer: ' 3000000000 '.
Light_years INT unsigned;
Valid integer: ' 3000000000 ',  illegal integer: '-24567 '.
float [(m,d)] floating-point type
Float represents a floating-point type, which is used to represent a more precise numeric type.
Rainfall FLOAT (4,2);
This variable can be used to indicate the average amount of precipitation in a year, and to decimal. FLOAT (4,2) indicates that a number can have a total of 4 digits, and 2 digits after the decimal point. Please see which of the following values can be represented by the above variable type:
42.35 is legal.
324.45 is illegal and will be modified to 324.5
2.2 is a valid numeric value.
34.542 is not valid and will be modified to 34.54
Note: Because float will round the numbers, it is recommended that you use decimal if you do not want the values to be changed arbitrarily.

Date Date Type
Used to hold date information, the default format is ' Yyyy-mm-dd ', the date range can be from ' 0000-00-00 ' to ' 9999-12-31 '. Declares a date-type variable the_date:the_date date;

Text/blob Text and Large objects
If the string is longer than 255, or if you want to save an article to a database, char and varchar cannot be used, and there is a text and blob type that can be saved with a string length of 255-65535 bytes. A blob is a large object that can hold binary data. Blob and text data types are the same, except that the text is case-insensitive and the blob is case-sensitive.

SET Fixed Type
A set is a string object that can have 0 or more values, each of which must be selected from a list of allowable values specified by the table creation. A set column consisting of more than one collection member is specified by a member separated by a comma (","). A set type can be up to 64 values.
Transport SET ("Truck", "wagon") not NULL;
After the above declaration, transport can have the following values:
""
"Truck"
"Wagon"
"Truck,wagon"

enum Enum type
An enum is the same data type as the Set property, but only one value is taken from the value.
Transport ENUM ("Truck", "wagon") not NULL;
After the above declaration, transport can have the following values:
""
"Truck"
"Wagon"
Records Records
A set of data types constitutes a record. A record can have only one data type, or it can be set as many as needed. One or more records make up a table.
Tables table
Before we can manipulate the database, we must create a table that can hold the data. You can create this by using the following methods:
mysql> CREATE TABLE Test (
> Name VARCHAR (15),
> Email VARCHAR (25),
> Phone_number INT,
> ID INT not NULL auto_increment,
> PRIMARY KEY (ID));
Output results:
Query OK, 0 rows affected (0.10 sec)
Mysql>
So your first watch is built.
The following options are available for all data types:
· Primary key value primary key. Primary key values are used to differentiate records because no two records can use the same primary key value. It is useful to use primary key values when you must maintain the uniqueness of a record.
· Auto_increment automatic increment. Columns that use this option automatically add a value of 1 to the column that is recorded in each additional record.
· NOT NULL Non-empty variable. Indicates that the column cannot be assigned a null value.
Cases:
Soc_sec_number INT PRIMARY KEY;
So the Soc_sec_number field cannot have duplicate values.
Id_number INT auto_increment;
Starting with 1, automatically adds the following values to the previous 1 on a per-order basis.


Table-related commands

We can use some of the table-related commands:


Show Table Contents

If you want to display all of the tables that exist in the database, you can use the command shows tables,mysql> show tables;

Display column 
Mysql> show columns from test;
Results:
Displays the columns and data associated with the table.
Now you have a basic understanding of how to create a table. Tables are composed of data types, and all data types form records.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.