3.数据库基本操作

数据库操作:

查看所有数据库 show databases;

使用数据库  use 数据库名;

查看当前使用的数据库  select database();

 

创建数据库

create database 数据库名charset=utf8;
例:create database python charset=utf8;

 

删除数据库

drop database 数据库名;
例:drop database python;

 

数据表

查看当前数据库中所有表  showtables;

 

查看表结构  desc 表名;

 

创建表

auto_increment表示自动增长

CREATE TABLE table_name(
    column1 datatype contrai,
    column2 datatype,
    column3 datatype,
    .....
    columnN datatype,
    PRIMARY KEY(one or morecolumns)
);

-- 例:创建班级表
create table classes(
    id int unsigned auto_incrementprimary key not null,
    name varchar(10)
);

-- 例:创建学生表create table students(
id int unsigned primary key auto_increment not null,
    name varchar(20) default'',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('
','','人妖','保密'),
    cls_id int unsigned default 0
)

修改表-添加字段

alter table 表名 add列名 类型;
例:alter table students add birthday datetime;

 

修改表-修改字段:重命名版

alter table 表名change 原名 新名 类型及约束;
例:alter table students change birthday birth datetime not null;

 

修改表-修改字段:不重命名版

alter table 表名modify 列名 类型及约束;
例:alter table students modify birth date not null;

 

修改表-删除字段

alter table 表名drop 列名;
例:alter table students drop birthday;

 

删除表

drop table 表名;
例:drop table students;

 

查看表的创建语句

show create table 表名;
例:show create table classes;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值