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;
總結
- 上一篇: python中文件读取操作及注意事项
- 下一篇: linux cmake编译源码,linu