Home Database Mysql Tutorial 新手该看的MYSQL操作_MySQL

新手该看的MYSQL操作_MySQL

Jun 01, 2016 pm 01:57 PM
article surface

       关于 PHP 教程的文章已经很多了,今天给大家介绍几个常用的MYSQL语句。

显示数据库或表:

    show databases;//然后可以use database_name;
    show tables;

更改表名:

    alter table table_name rename new_t;

添加列 :

    alter table table_name add column c_n column attributes;

删除列:

    alter table table_name drop column c_n;

创建索引:

    alter table c_table add index (c_n1,c_n2);
    alter table c_table add unique index_name(c_n);
    alt关于 PHP 教程的文章已经很多了,今天给大家介绍几个常用的MYSQL语句。

显示数据库或表:

    show databases;//然后可以use database_name;
    show tables;

更改表名:

    alter table table_name rename new_t;

添加列 :

    alter table table_name add column c_n column attributes;

删除列:

    alter table table_name drop column c_n;

创建索引:

    alter table c_table add index (c_n1,c_n2);
    alter table c_table add unique index_name(c_n);
    alter table c_table add primary key(sid);

删除索引:

    alter table c_table drop index c_n1;

更改列信息:

    alter  table t_table change c_1 c_1 varchar(200);
    alter table t_table modify 1 c_1 varchar(200);

insert插入语句:

    insert into table_name (c_1,c_2)
        values ('x1',1);

update语句:

    update  table_name set c_1 =1 where c_2=3;

删除数据库或者表:

    drop table table_name;
    drop database database_name;//使用mysql_drop_db()可以删除的.
 如果MYSQL运行在服务器上,而你在客户端需要连接的化,需要按如下方式进行连接:

  shell>mysql -h host -u user -p

  Enter password:************

  host和user分别代表MySQL服务器运行的主机名和MySQL帐户名.添写时写上对应的机器的名称和MySQL用户名. *********代表你的密码.

  如果有效,你会看到如下提示:

  Welcome to the MySQL monitor. Commands end with ; or /g.

  Your MySQL connection id is 7 to server version: 5.0.18-nt

  Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

  mysql>

  mysql>提示符告诉你该输入命令了.

  有些时候MySQL会允许你匿名登陆到服务器上,此时直接输入mysql即可.

  shell> mysql

  成功连接后,可在mysql>提示下输入quit随时退出:

  mysql> quit

  Bye

  下面显示一个简单的查询实例,这是一个简单命令,要求得到服务器的版本号和当前日期:

  mysql> select version(), current_date;

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

  | version() | current_date |

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

  | 5.0.18-nt | 2006-07-29 |

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

  1 row in set (0.00 sec)

  mysql>

  在这里我们要明白:

  一个命令通常由SQL语句组成,随后跟着一个分号.

  MySQL的显示结果用表格(行和列)方式输出.第一行为列的标签,随后行为查询的结果.通常列标签是你查询的数据库表的列名,但若检索的是一表达式而非列值(如上一个例子),通常用表达式本身来标记列.

  随后它会显示返回了多少行以及查询时间,它提供了服务器性能的一个大致估计.它表示的是时钟时间并非CPU或机器时间.

  能够以大小写输入关键字,即在MySQL中大小写是等价的,但在一个查询语句中,要求大小写统一.

  以下是另一个查询:

  mysql> SELECT SIN(PI()/4), (4+1)*5;

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

  | SIN(PI()/4) | (4+1)*5 |

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

  | 0.70710678118655 | 25 |

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

  1 row in set (0.02 sec)

  mysql>

  可以在一行上输入多条语句,如:

  mysql> SELECT VERSION(); SELECT NOW();

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

  | VERSION() |

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

  | 5.1.2-alpha-log |

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

  1 row in set (0.00 sec)

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

  | NOW() |

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

  | 2005-10-11 15:15:00 |

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

  1 row in set (0.00 sec)

  mysql>

  较长的命令可以分多行输入. MySQL是通过分号来判断语句是否结束,而不是换行.

  下面是一个简单的多行语句的例子:

  mysql> select

  -> user(),

  -> current_date;

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

  | USER() | CURRENT_DATE |

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

  | jon@localhost | 2007-10-11 |

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

  mysql>

  在这个例子中,输入多行查询的第一行后,你可以看提示符变为 ->,这就是说MySQL没有找到语句结束标识正在等待剩余的输入部分. 如果不想执行正在输入过程的一个命令,输入/c 取消它:

  mysql> select

  -> user()

  -> /c

  mysql>

  下面显示的是各个提示符以及它们所表示的MySQL的状态:

  提示符 含义

  mysql> 准备好接受新命令。

  -> 等待多行命令的下一行。

  '> 等待下一行,等待以单引号(“'”)开始的字符串的结束。

  "> 等待下一行,等待以双引号(“"”)开始的字符串的结束。

  `> 等待下一行,等待以反斜点(‘`’)开始的识别符的结束。

  /*> 等待下一行,等待以/*开始的注释的结束。
er table c_table add primary key(sid);

删除索引:

    alter table c_table drop index c_n1;

更改列信息:

    alter  table t_table change c_1 c_1 varchar(200);
    alter table t_table modify 1 c_1 varchar(200);

insert插入语句:

    insert into table_name (c_1,c_2)
        values ('x1',1);

update语句:

    update  table_name set c_1 =1 where c_2=3;

删除数据库或者表:

    drop table table_name;
    drop database database_name;//使用mysql_drop_db()可以删除的.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! Mar 15, 2024 pm 04:13 PM

1. How can you make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! 1. Activate basic rights and interests: original articles can earn profits by advertising, and videos must be original in horizontal screen mode to earn profits. 2. Activate the rights of 100 fans: if the number of fans reaches 100 fans or above, you can get profits from micro headlines, original Q&A creation and Q&A. 3. Insist on original works: Original works include articles, micro headlines, questions, etc., and are required to be more than 300 words. Please note that if illegally plagiarized works are published as original works, credit points will be deducted, and even any profits will be deducted. 4. Verticality: When writing articles in professional fields, you cannot write articles across fields at will. You will not get appropriate recommendations, you will not be able to achieve the professionalism and refinement of your work, and it will be difficult to attract fans and readers. 5. Activity: high activity,

How to implement the statement to view table data in MySQL? How to implement the statement to view table data in MySQL? Nov 08, 2023 pm 01:40 PM

Title: Statements and specific code examples for viewing table data in MySQL MySQL is an open source relational database management system that is widely used in applications of all sizes. In MySQL, viewing table data is a very basic operation. The following will introduce how to implement this operation through specific statements and code examples. First, we will introduce the statements and specific code examples for viewing table data through the MySQL command line tool. Suppose we have a table named "employees", the following is the pass

How to add article in HTML5? How to add article in HTML5? Sep 12, 2023 am 11:37 AM

In this article, we will learn how to add articles in HTML5. One of the new segmentation elements in HTML5 is the tag. Articles are represented in HTML using tags. More specifically, the content contained within the element is different from the rest of the site's content (even though they may be related). Let us consider the following example to understand how to add an article in HTML5 Example 1 In the following example, we are using inline styles in the article element. <!DOCTYPEhtml><html><body><articlestyle="width:300px;border:2pxsolidgray;padding:

Interpretation of Vitalik's new article: Why does Rollup, whose blob space is not used efficiently, fall into development difficulties? Interpretation of Vitalik's new article: Why does Rollup, whose blob space is not used efficiently, fall into development difficulties? Apr 01, 2024 pm 08:16 PM

How to understand @VitalikButerin’s new article’s thoughts on Ethereum’s expansion? Some people say that Vitalik’s order for Blob Inscription is outrageous. So how do blob packets work? Why is the blob space not being used efficiently after the upgrade in Cancun? DAS data availability sampling in preparation for sharding? In my opinion, the performance of Cancun is usable after the upgrade, and Vitalik is worried about the development of Rollup. Why? Next, let me talk about my understanding: As I have explained many times before, Blob is a temporary data package that is decoupled from EVM calldata and can be directly called by the consensus layer. The direct benefit is that EVM does not need to access the Blob when executing transactions. data, thus resulting in lower execution layer computations

Detailed method for sending articles and recordings at the same time via WeChat Detailed method for sending articles and recordings at the same time via WeChat Mar 26, 2024 am 09:16 AM

1. Open your phone, click on the WeChat software, and enter the WeChat home page settings. 2. Find [My] in the lower right corner of WeChat, open it, and enter the [My] page. 3. Click Collection and then open a new page.

How to implement the statement of renaming table in MySQL? How to implement the statement of renaming table in MySQL? Nov 08, 2023 pm 12:11 PM

MySQL is a commonly used relational database management system that supports the operation of renaming tables. Normally, renaming a table carries certain risks, so you should be very careful when performing this operation. In this article, we will explore how to implement the rename table statement in MySQL and provide detailed code examples. In MySQL, you can use the ALTERTABLE statement to rename a table. The following is the basic syntax of the ALTERTABLE rename statement: ALTERTABLEo

How to set read-only permissions on a table in Oracle database? How to set read-only permissions on a table in Oracle database? Mar 06, 2024 pm 03:03 PM

In the Oracle database, setting read-only permissions on tables is a very important operation, which can protect data security and prevent misoperations. The following will introduce how to set read-only permissions on tables in an Oracle database and provide specific code examples. First, we need to understand that in the Oracle database, users obtain permissions on tables through authorization. Table permissions include SELECT (query), INSERT (insert), UPDATE (update) and DELETE (delete) operations. Here we will introduce

Use MySQL to create a recommendation system table to implement the recommendation system function Use MySQL to create a recommendation system table to implement the recommendation system function Jul 02, 2023 am 10:01 AM

Use MySQL to create a recommendation system table to implement the recommendation system function. The recommendation system is a system used to recommend personalized content to users based on their preferences and behaviors. In the recommendation system, the database is a key component, which stores information such as user data, item data, and user-item interaction data. As a commonly used relational database management system, MySQL can be used to create recommendation system tables and implement the functions of the recommendation system. This article will introduce how to use MySQL to create a recommendation system table and demonstrate it through code examples.

See all articles