mysql数据库表的基本操作sql语句总结
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:设定默认校对集
创建数据库示例:
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