mysql資料庫表的基本操作sql語句總結

來源:互聯網
上載者:User

標籤:

1,命令列登入命令

mysql -h localhost -u root -p

 

  1. C:\Users\lenovo>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

 

mysql資料庫表的基本操作sql語句總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.