当前位置:   article > 正文

数据库操作_您好,您的女儿名字叫linda;drop databases 吗

您好,您的女儿名字叫linda;drop databases 吗

– 连接数据库

mysql -uroot -p

–退出数据库
exit/quit

–sql语句最后需要有分号;结尾
–显示数据库版本
select version();

–显示时间
select now();

–查询所有数据库
show databases;

–DDL(数据定义语句)
–创建数据库
create database python04;
create database python04 charset=utf8;

–查看创建数据库的语句
show create database python04;

–查看当前数据库
select database();

–使用数据库
– use 数据库的名字
use python04;

–删除数据库
–drop databases 数据库名;

数据表的操作
–查看当前数据库中所有表
show tables;

–创建表
–auto_increment表示自动增长
–not null 表示不能为空
–primary key 表示主键
–default 默认值

–create table 数据表名字(字段 类型 约束[,字段 类型 约束]);
create table demo1(id int,name varchar(30));

create table demo2(
id int primary key not null auto_increment,
name varchar(30)
);

查看表结构
–desc 数据表的名字
desc demo2;

–创建students表(id,name,age,high,gender,cls_id)
create table students(
id int primary key not null auto_increment,’
name varchar(30),
age tinyint unsigned default 18,
high decimal(5,2),
gender enum(‘男’,‘女’,‘保密’) default ‘保密’,
cls_id int
);

– 创建classes表(id,name)
create table classes(
id int primary key not null auto_increment,
name varchar(30)
);

–查看表的创建语句
–show create table 表名字;
show create table students;

–修改表-添加字段
–alter table 表名 add 列名 类型;
alter table students add birthday date;

–修改表-修改字段:重命名版
– alter table 表名 change 原名 新名 类型及约束
alter table students change birthday birth date default ‘1990-1-1’

–修改表-修改字段:不重命名版
– alter table 表名 modify原名 新名 类型及约束
alter table students modify birthday date default ‘1990-1-1’

–修改表-删除字段
–alter table 表名 drop 列名;
alter table students drop high;

–删除表
–drop table 表名;
–drop databases 数据库;

–增删改查(curd)
– 增加
– 全列插入
– insert into 表名 values(…)
– 向classes 表中插入 一个班级 id name
insert into classes values(1,‘大神班’);

-- 向students表插入一个学生信息

field     type                           Null     key      Default        EXtre
id        int(11)                         No      PRI       NULL       auto_increment
name     varchar(30)                      YES               NULL   
age     tinyint(3)unsigned                YES                0
gender   enum('男','女','中性','保密')     YES                保密
cls_id    int(10) unsigned                YES                 1997-01-01
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

– 主键字段 0 null default 来占位
insert into students values(0,‘琳琳’,18,‘女’,1,‘1990-1-1’);
insert into students values(null,‘小东’,19,‘男’,1,‘19

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/836905
推荐阅读
相关标签
  

闽ICP备14008679号