標籤:
1,命令列登入命令
mysql -h localhost -u root -p
- C:\Users\lenovo>mysql -u root -p
- Enter password: *****
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 5
- Server version: 5.5.28 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
2,建立資料庫
查看協助:\h create database 或者 help create database
- mysql> \h create database
- Name: ‘CREATE DATABASE‘
- Description:
- Syntax:
- CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
- [create_specification] ...
-
- create_specification:
- [DEFAULT] CHARACTER SET [=] charset_name
- | [DEFAULT] COLLATE [=] collation_name
-
- CREATE DATABASE creates a database with the given name. To use this
- statement, you need the CREATE privilege for the database. CREATE
- 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
- mysql> create table tb3
- -> (id int(10) primary key,
- -> name varchar(20));
- mysql> create table tb4
- -> (id int(10),
- -> name varchar(20),
- -> primary key (id));
聯合主鍵
- mysql> create table tb5
- -> (id int(10),
- -> name varchar(20),
- -> primary key(id, name));
外鍵約束
- mysql> create table tb7
- -> (
- -> id int(11),
- -> b_id int (11),
- -> name varchar(45),
- -> primary key (id),
- -> foreign key(b_id) references tb4(id)
- -> );
刪除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語句總結