成都10班-苏燕 发表于 2021-11-21 20:23:58

mysql数据库指令

yum install mysql ==》    安装mysql数据库客户端
yum install mysql-server ==》   安装mysql数据库服务器
service mysqld start==》 启动mysql数据库
service mysqld stop ==》 停止MySQL数据库
service mysqld status ==》查看MySQL数据库的状态
service mysqld restart ==》重启MySQL数据库


mysql -uroot -p ==》进入MySQL数据库,-uroot 表示使用root用户,-p表示密码。首次进入不用输入密码
mysqladmin -uroot password "123456" ==》 给root用户设置访问密码
设置密码后使用 mysql -uroot -p 访问数据库,提示输入密码;或者使用 mysql -uroot -p123456访问数据库
show databases; ==》查看MySQL数据库里面所有的数据库;查看所有库的时候,会自动从当前库里退出来
create dtabase dcs10; ==》创建一个名为dcs10的数据库空库
use dcs10; ==》进入到dcs10数据库里
show tables;==》查看进入的数据库里所有的表


creat table test(id int(20) primary key auto_increment,name varchar(20) not null,
score float(20,2),phone bignit(20) default 15388887777,time date);   ==》创建一个名为test的表
desc +表名==》查看表结构/查看表定义

create database dcs11; ==》创建一个dcs11空库
drop database dcs11;==》 删除dcs11这个空库

alter table test rename test1; ==》将test表名更改为test1

alter table test change id sid int(20);==》 把id字段改为sid,并且去掉自增长
alter table test change sid sid int(20) quto_increment; ==》给sid字段增加自增长约束

alter table test add class int(20) first; ==》给test表添加一个class字段并放在最前面
alter table test add sex varchar(20) after class;==>> 添加一个字段sex放在class后面

alter table test drop sex; ==》删除sex字段

alter table test add(sex varchar(20),age int(20)); ==》同时添加两个字段sex和age,默认放在表字段最后面
alter table test drop sex,drop age; ==》同时删除这两个字段

alter table test modify name varchar(20)after class; ==》将name字段放在class字段后面

drop table test1 ==》删除表test1

alter table test change sid sid int(20); ==》先删除sid 字段的自增长约束
alter table test drop primary key; ==》再删除主键约束

alter table test change sid sid int(20) primary key auto_increment; ==》给sid 字段添加主键约束和自增长约束

insert into test (sid,name,class,time)values(1,"xiaoyi",2021,"2021-11-21"); ==》给test表所有字段都插入数据
select * from test ==》查询test表所有的数据,*号表示所有字段

insert into test(name)values("xiaoer") ==》单独给name字段插入值xiaoer
sid字段因主键约束,不能插入重复值

alter table test change name name varchar(20) not null; ==》给name字段增加不能为空的属性
insert into test (name)vlaue(null); ==》给name字段插入空值

页: [1]
查看完整版本: mysql数据库指令