Mysql simple tutorial (2), mysql simple tutorial

Source: Internet
Author: User
Tags mysql tutorial

Mysql simple tutorial (2), mysql simple tutorial

Next, a simple tutorial on Mysql in Linux (1)

Field parameters are constructed in the form of "(field name 1 Data Type 1, field name 2 Data Type 2.

Mysql data types are commonly used. You can search for them online for more detailed information.

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

Type description

CHAR 1 ~ A fixed length string of 255 characters. The length must be specified at the time of creation. Otherwise, MySQL is assumed to be CHAR (1)

VARCHAR variable length, up to 255 bytes. If you specify VARCHAR (n) during creation, you can store ~ Variable-length string with n characters

Variable-length TEXT with a maximum length of 64 KB

LONGTEXT is the same as Text, and the maximum length is 4 GB (plain Text, usually not 4 GB)

ENUM accepts a string of a predefined set consisting of up to 64 K strings

SET accepts zero or multiple predefined sets of up to 64 K strings

INT [(M)] 4 bytes (integer, signed value:-2147683648 to 2147683647 (-2 ^ 31 to 2 ^ 31-1) unsigned value: 0 to 4294967295 (0 to 2 ^ 32-1 ))

DATE 4 bytes (display DATE in the format of year, month, and day, range: 1000-01-01--9999-12-31)

TIME 3 bytes (display TIME in the format of hour, minute, second, range:-838: 59: 59--838: 59: 59: 59)

DATETIME 8 bytes (display date and time, range: 1000-01-01 00:00:00 -- 9999-12-31 23:59:59)

BLOB stores binary data types. The maximum length is 64 KB.

Note: (1) the TIME range has such a large range. In particular, the time value can be negative. This is designed for mysql to meet the offset of two dates and times.

(2) BLOB binary data is an important data type, because all images, videos, and audio files are stored in binary format.

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

After creating a new table, you can roughly view the table structure. The command is:> Desc table name; Or> Show columns from table name;

The table structure includes the attribute name, data type, whether it is null, and whether to set the default value.

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

You can also precisely view the data in the table. The names used are:> select field name 1, field name 2, and field name 3 ...... from table name;

In practice, we often use the following commands to view the content of the command to be lazy. In fact, they are all variants or extensions of the above commands, so we understand the commands of the previous line, it is easy to understand the following commands.

Such as this command:> Select * from Table Name;

For example, to view rows n to m in the table:> Select * fromTable Name Order by field name limit n-1, m;

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

Insert data to the table with the following command:> insert into Table Name (field name 1, field name 2, field name 3 ......) values (value of field name 1, value of field name 2, value of field name 3 ......);

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

After adding a table, it is necessary to delete the data in the table.:> Delete fromTable Name WhereExpression;

The table name locates the table, and the expression locates the specific location to be deleted. The expression is in the form of an equation related to the field name and the data in the table;

For example, name = "Bob", age = 12, and high = 180 are all related to field names.

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

Delete and destroy are often the easiest. How can I modify the content? For example, if you accidentally input an error during the input process, you must modify it later:

Use commands:> Update table name: set field name = "New Value" where expression;

Change the age field named "Ason" and the Value 15 to 13;

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

Well! It seems that you can gradually learn to operate the content in the database. Don't worry, there are still many things to learn, there is no end to learning, it is the most important to keep a hunger and thirst.

Now we can see that the above table has three fields: name, age, and height. Perfection is always constantly changing to perfect, assume that you need to add/delete an address field.

Use the command to add a field:> Alter table name add field name other data types;(Others include setting of default values, etc)

To delete a field, run the following command::> Alter table Name drop field name;

Similar structures include: adding an index, adding a primary keyword index, and deleting an index etc. Please try again;

Add Index Command:> Alter table name add index name (field name 1, field name 2, field name 3 .......);

Key sub-index addition command:> Alter table name add primary key (field name );

Index deletion command:> Alter table Name drop index name;

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

To modify the table name, run the following command:> rename table, the original table name to the new table name;

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

A table is messy and unordered. For large tables in a large database, looking for something in it is just a haystack, therefore, sorting the content in the table is more efficient in searching for data.

Use commands:> Select field name 1, field name 2 ...... from table name order by field name 1, field name 2 ......;

(The first field is the content of the field to be displayed, and the second field is the sorting field. Do not confuse it !)

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

Okay! In the next section, merge and back up tables.

The above is a simple Mysql tutorial (II). I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.