mysql增删改查语句

MySQL的增删改查(CRUD)操作是数据库管理的基础,以下是具体的SQL语句示例:

  1. 创建数据库
    CREATE DATABASE database_name;
    ```

2. **删除数据库** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    DROP DATABASE database_name;
    ```

3. **使用数据库** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    USE database_name;
    ```

4. **显示所有数据库** <b class="card40_249__sup_a7f6" data-sup="sup">6</b>:

```sql
    SHOW DATABASES;
    ```

5. **创建数据表** <b class="card40_249__sup_a7f6" data-sup="sup">2</b>:

```sql
    CREATE TABLE table_name (
        column1 datatype1,
        column2 datatype2,
        ...
    );
    ```

6. **添加列** <b class="card40_249__sup_a7f6" data-sup="sup">7</b>:

```sql
    ALTER TABLE table_name ADD COLUMN column_name datatype;
    ```

7. **删除列** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    ALTER TABLE table_name DROP COLUMN column_name;
    ```

8. **修改列** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    ALTER TABLE table_name CHANGE COLUMN column_name new_column_name datatype;
    ```

9. **删除表** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    DROP TABLE table_name;
    ```

10. **插入数据** <b class="card40_249__sup_a7f6" data-sup="sup">8</b>- 插入所有列<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
        ```

- 插入部分列<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
        ```

- 多行同时插入<b class="card40_249__sup_a7f6" data-sup="sup">8</b>:

```sql
        INSERT INTO table_name SELECT column1, column2, ... FROM another_table;
        ```

11. **查询数据**- 查询所有列<b class="card40_249__sup_a7f6" data-sup="sup">8</b>:

```sql
        SELECT * FROM table_name;
        ```

- 查询部分列<b class="card40_249__sup_a7f6" data-sup="sup">8</b>:

```sql
        SELECT column1, column2 FROM table_name;
        ```

- 带条件查询<b class="card40_249__sup_a7f6" data-sup="sup">8</b>:

```sql
        SELECT * FROM table_name WHERE condition;
        ```

- 多条件查询<b class="card40_249__sup_a7f6" data-sup="sup">1</b>:

```sql
        SELECT * FROM table_name WHERE column1 IN (value1, value2, ...);
        ```

12. **修改数据** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>- 修改所有列<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        UPDATE table_name SET column1 = value1, column2 = value2, ...;
        ```

- 修改某个条件的数据<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        UPDATE table_name SET column1 = value1 WHERE condition;
        ```

- 修改多个列<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
        ```

13. **删除数据** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>- 删除所有数据<b class="card40_249__sup_a7f6" data-sup="sup">6</b>:

```sql
        DELETE FROM table_name;
        ```

- 删除部分数据<b class="card40_249__sup_a7f6" data-sup="sup">9</b>:

```sql
        DELETE FROM table_name WHERE condition;
        ```

14. **分组统计** <b class="card40_249__sup_a7f6" data-sup="sup">5</b>:

```sql
    SELECT column1, COUNT(*) as count FROM table_name GROUP BY column1;
    ```

这些SQL语句涵盖了MySQL中数据库的基本操作,适用于各种场景。建议在实际应用中根据具体需求选择合适的操作方法。
Top