zoukankan      html  css  js  c++  java
  • mysql数据库表的基本操作sql语句总结

    1,命令行登录命令

    mysql -h localhost -u root -p

    1. C:Userslenovo>mysql -u root -p  
    2. Enter password: *****  
    3. Welcome to the MySQL monitor.  Commands end with ; or g.  
    4. Your MySQL connection id is 5  
    5. Server version: 5.5.28 MySQL Community Server (GPL)  
    6.   
    7. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.  
    8.   
    9. Oracle is a registered trademark of Oracle Corporation and/or its  
    10. affiliates. Other names may be trademarks of their respective  
    11. owners.  
    12.   
    13. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  

    2,创建数据库

    查看帮助:h create database 或者 help create database

    1. mysql> h create database  
    2. Name: 'CREATE DATABASE'  
    3. Description:  
    4. Syntax:  
    5. CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name  
    6.     [create_specification] ...  
    7.   
    8. create_specification:  
    9.     [DEFAULT] CHARACTER SET [=] charset_name  
    10.   | [DEFAULT] COLLATE [=] collation_name  
    11.   
    12. CREATE DATABASE creates a database with the given name. To use this  
    13. statement, you need the CREATE privilege for the database. CREATE  
    14. SCHEMA is a synonym for CREATE DATABASE.  

    [option1, option2,...]代表可选的 {option1 | option2}代表要选择其中的某一项

    SCHEMA是DATABASE的同义词

    IF NOT EXISITS:如果不存在同名数据库才会执行创建语句;如果不添加此选项,而且存在同名数据库的话,则会报错

    创建参数:

    [DEFAULT] CHARACTER SET [=] charset_name:设定默认字符集

    [DEFAULT] COLLATE [=] collation_name:设定默认校对集

    参考在SQL语句中使用COLLATE

    创建数据库示例:

    mysql> create database if not exists test_db  

        -> default character set = utf8;  


    查看已创建的数据库的定义

    mysql> show create database test_db;  

    查看已创建哪些数据库

    mysql> show databases;  

    3,删除数据库

    mysql> drop database test_db;

     

    主键约束 PRIMARY KEY

    1. mysql> create table tb3  
    2.     -> (id int(10) primary key,  
    3.     -> name varchar(20));  
    1. mysql> create table tb4  
    2.     -> (id int(10),  
    3.     -> name varchar(20),  
    4.     -> primary key (id));  

    联合主键 

    1. mysql> create table tb5  
    2.     -> (id int(10),  
    3.     -> name varchar(20),  
    4.     -> primary key(id, name));  


    外键约束

      1. mysql>  create table tb7  
      2.     ->  (  
      3.     ->  id int(11),  
      4.     ->  b_id int (11),  
      5.     ->  name varchar(45),  
      6.     ->  primary key (id),  
      7.     ->  foreign key(b_id) references tb4(id)  
      8.     -> );  

    删除mysql一个表中所有数据实例。

    truncate table mytable;

    删除表 DROP TABLE <table_name>

    附录:

    MySQL数据库基本操作   http://blog.csdn.net/netoscoder/article/details/10662483

     MySQL数据库表的基本操作——创建表CREATE TABLE  http://blog.csdn.net/netoscoder/article/details/10716253

    MySQL数据库表的基本操作——修改表ALTER TABLE,删除表 http://blog.csdn.net/netoscoder/article/details/10731609

     

  • 相关阅读:
    sql 在日期范围内搜索
    js 处理日期时间字符串显示的方法
    matlab练习程序(并行计算)
    C++程序运行时间
    matlab练习程序(KNN,K最邻近分类法)
    多媒体指令(像素处理)
    ubuntu启动/重启/停止apache
    matlab练习程序(matlab调用c/c++)
    我的vim设置
    matlab练习程序(c/c++调用matlab<engine>)
  • 原文地址:https://www.cnblogs.com/scw2901/p/4345030.html
Copyright © 2011-2022 走看看