您的位置:首页 > 数据库

数据库基本操作

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 安装 使用