数据库基本操作
2017-12-11 14:21
281 查看
查看所有数据库
show databases;
使用数据库
use 数据库名;
查看当前使用的数据库
select database();
创建数据库
create database 数据库名 charset=utf8;
例:
create database python charset=utf8;
删除数据库
drop database 数据库名;
#######
查看当前数据库中所有表
show tables;
查看表结构
desc 表名;
创建表
auto_increment表示自动增长
create table 表名(列 类型 约束,...);
例:创建班级表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(10),
isdelete bit default 0
);
例:创建学生表
create table students(
id int unsigned auto_increment primary key not null,
name varchar(10) not null,
gender bit default 1,
hometown varchar(20),
clsid int unsigned,
isdelete bit default 0,
foreign key(clsid) references classes(id)
);
修改表-添加字段
alter table 表名 add 列名 类型;
例:
alter table students add birthday datetime;
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change name name1 varchar(20) not null;
修改表-修改字段:不重命名版
alter table 表名 modify 列名 类型及约束;
例:
alter table students modify name1 varchar(10) not null;
修改表-删除字段
alter table 表名 drop 列名;
例:
alter table students drop birthday;
删除表
drop table 表名;
例:
drop table students;
查看表的创建语句
show create table 表名;
例:
show create table classes;
show databases;
使用数据库
use 数据库名;
查看当前使用的数据库
select database();
创建数据库
create database 数据库名 charset=utf8;
例:
create database python charset=utf8;
删除数据库
drop database 数据库名;
#######
查看当前数据库中所有表
show tables;
查看表结构
desc 表名;
创建表
auto_increment表示自动增长
create table 表名(列 类型 约束,...);
例:创建班级表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(10),
isdelete bit default 0
);
例:创建学生表
create table students(
id int unsigned auto_increment primary key not null,
name varchar(10) not null,
gender bit default 1,
hometown varchar(20),
clsid int unsigned,
isdelete bit default 0,
foreign key(clsid) references classes(id)
);
修改表-添加字段
alter table 表名 add 列名 类型;
例:
alter table students add birthday datetime;
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change name name1 varchar(20) not null;
修改表-修改字段:不重命名版
alter table 表名 modify 列名 类型及约束;
例:
alter table students modify name1 varchar(10) not null;
修改表-删除字段
alter table 表名 drop 列名;
例:
alter table students drop birthday;
删除表
drop table 表名;
例:
drop table students;
查看表的创建语句
show create table 表名;
例:
show create table classes;
相关文章推荐
- drupal 中基本的数据库操作
- 数据库基本操作小摘(代码由mysql-essential-5.1.50-win32测试通过)
- 机房收费系统——数据库基本操作
- 测试工程师应该知道的数据库基本操作(增删改查)
- android SQLite数据库的基本操作
- jpa数据库增删改查基本操作
- JAVA数据库基本操作
- 数据库的基本操作
- B树(B-Tree)的由来、数据结构、基本操作以及数据库索引的应用
- 数据库基本操作(创建表和添加触发器)
- C# 数据库的基本操作(sqlserver)
- mysql 数据库基本操作
- 数据库的基本操作
- Mongodb-非结构化数据库的基本操作
- JDBC连接数据库和基本操作
- 数据库基本操作语法归纳总结
- 二、数据库的基本操作
- asp基本操作数据库语句示例
- JDBC学习笔记(5):数据库的基本操作CRUD
- iOS FMDB数据库基本操作