MySQL的使用

用命令行来执行数据库(DDL数据库定义语言)

(1)输入登录密码(连接的密码)

mysql -P3306 -uroot -p

修改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码'; 

(2)显示MySQL中所有的数据库

show databases;

(3)退出MySQL

使用 quit、exit、\q 退出MySQL

(4)创建数据库(create database 数据库名) 

create datebase school

创建数据库时,判断是否具有同名数据库再创建

create datebase if not exists school

创建数据库后,为数据库设置字符编码

alter database school character set utf8

(5)删除数据库(drop database 数据库名)

drop database school

(6)使用某个数据库(user 数据库名)

注意:进入到该数据库,例如你新建表的时候要先写这句话,这样保证了我的表创建在该数据库下,反之会默认放置在默认的数据库中  

use school

(7)创建表(create table 表名 (列名1 数据类型,.......))

create table if not exists user(
	id int not null primary key,
	username varchar(40),
	password varchar(50),
	power int not null
)

创建表时,是id自动递增用 auto_increment

create table if not exists user(
	id int not null auto_increment primary key,
	username varchar(40),
	password varchar(50),
	power int not null
)

(8)显示选择的数据库中所有的表 

show tables

(9)查看表里面的详细信息(desc 表名)

desc user

(10)删除表(drop table 表名)

drop table user

(11)删除一个表的主键(alter table 表名 drop primary key)

alter table user drop primary key

(12)往表中添加一列(alter table 表名 add 要添加的列名 数据类型)

alter table user add address varchar(50)

(13)往id列后面添加一列

alter table user add phone varchar(50) after id

(14)增加一列,使其位于第一列

alter table user add id1 int not null first

(15)删除某一列(alter table 表名 drop 列名)

alter table user drop id1

(16)修改表字段

   a、使用change可以修改字段名称和该字段的数据类型(alter table 表名 change 字段名 新字段名 新数据类型)

alter table user change id userid varchar(30)

   b、使用Modify只能修改数据类型(alter table 表名 modify 字段名 新数据类型)

alter table user modify password varchar(30)
posted @ 2019-03-29 16:48  Lomen~  阅读(550)  评论(0编辑  收藏  举报