关于mysql中常用命令

本文提供了MySQL的基本操作命令,包括连接数据库、创建与删除数据库和表、查询与修改数据、事务处理以及约束定义,如默认值、非空、主键和自增长等。

命令:

#1.连接数据库服务器的命令
mysql -uroot -p
#退出命令
exit;
​
show databases; #展示库
create database 库名;#新建库
drop database 库名; #删除库
​
use 库名; #选中库
show tables;#查表
create table 表名(字段1 数据量类型 , 字段2 数据类型,...)#建表
drop table 表名;#删表
​
desc 表名; #查表结构
alter table 表名 drop 字段; #删字段
alter table 表名 add 字段 数据类型;#添加字段  decimal(8,2) 关键字
alter table 表名 add 字段 数据类型 after 字段;#指定添加到哪里
alter table 表名 modify 字段 新数据类型; #修改数据类型
alter table 表名 change 字段 新字段 新数据类型 ;#字段和数据类型一起改
​
insert into 表名 values (值1,值2,..);#表中添加数据
select * from 表名;#查看表中数据
insert into 表名 (字段1,字段2,..) values (值1,值2,..);#指定列插入数据
delete from 表名 where 条件;#删除数据  多条:where 字段 in (值..);
update 表名 set 列名称 =新值 where 列名称 = 某值;#修改数据
​
事务操作:set autocommit=0;
         sql1
         sql2
         commit(提交) , rollback(回滚);
 查询所有的数据
    select * from person;
    select id, name, age from person;
    select id as "编号", name as "姓名" from person;
    
    
    查询指定的数据,按照规定对数据进行操作
    
    运算符:
        where 字段 运算符 值
        =    >   <  !=    >=   like  
        
    逻辑运算符
        and (&&)  or(||)
        
    对数据进行排序
    order by  字段  排序的类型;
    select * from perso order by salary asc;//升序 默认  asc 可以省略的
    select * from perso order by salary desc;//降序
    
    在什么之间
    between
    
    模糊查询
    like
    select * from person where name like "%苏";
    显示输出数据
    limit 数字
    limit 数字1, 数字2;
    数字1:偏移量
    数字2:个数       

关于约束的几个重要sql语句

默认值:

mysql> create table person1 (
    -> id int,
    -> name varchar(32),
    -> country varchar(32) default "   " #在一个数据类型的后面 跟一个默认值
    -> );

非空

mysql> create table person3 (
    -> id int not null,
    -> name varchar(32) unique not null
    -> );

主键

mysql> create table person4(
    -> id int primary key ,
    -> name varchar(32)
    -> );

自增长

mysql> create table person5 (
    -> id int primary key auto_increment,
    -> name varchar(32)
    -> );

唯一

mysql> create table person3 (
    -> id int not null,
    -> name varchar(32) unique not null
    -> );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值