关于数据库的一些基本命令(一)

关于数据库的一些基本命令(一)  

环境:centerOS,mysql

一.mysql数据库的启动

1.链接数据库: mysql -uroot -p

2.退出数据库:quit/exit/ctrl+d

3.展示当前所有的数据库show databases;

4.显示当前数据库的时间: select now();

5.显示数据库的版本: select version();

6.创建数据库: create database test; create database [数据库名]

创建的数据库的编码默认使用拉丁,如果要指定创建的数据库的编码则使用下面一条语句:

create database newtest default charset=utf8;

7.删除数据库: drop database test; drop database [数据库名]

8.使用一个数据库:use [数据库名]

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

二、数据表的操作

1.查看当前数据库中的表:show tables;

2.创建数据库表create table student(id int, name varchar(30) );

创建带条件约束的表:

create table teacher(
    id int primary key not null, 
    name varchar(30)
);
create table students(
    id int unsigned not null auto_increment primary key,
    name varchar(30),
    age tinyint unsigned,
    high decimal(5,2),
    gender enum('男','女') default '男',
    cls_id int unsigned
);

3.查看数据表结构:desc student;

4.插入数据:insert into students values(0, 'hrr', 18, 160, '女', '110');

5.对数据表添加新的字段: alter table students add birthday datetime;

datetime表示年月日时分秒

6.修改数据表中的字段属性:

(1)修改字段,不重名版:alter table 数据表名 modify 列名 类型及约束;

alter table students modify birthday date;

(2)修改字段重命名:alter table 数据表名 change 原名 新名 类型及约束;

alter table students change birthday birth date;

7.删除字段:alter table 数据表名 drop 字段名;

alter table students drop high;

三、数据表的增删改查

(1)插入数据

insert into 表名 values(对应的字段的值)

insert into students values(0, 'hrr', 18, 160, '女', '110');

(2)部分插入

insert into 表名(字段名) values(字段对应的值)。

insert into students(name, gender) values('小龙女','女');

(3)修改表中的记录

update 表名 set 字段 where 条件;

update students set gender='男' where id = 2;

(4)查询数据

select * from students where id=3;

select name,gender from students;

select name as 姓名,gender as 性别 from students;

select s.name,s.gender from student as s;

(5)消除重复行

distinct 字段:select distinct gender from student

(6)删除数据

delete from students; --删除数据表中所有的元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值