輸入命令注意:
1、命令結束符號是分號;
2、所有的符號都是英文半角
3、只有遇到分號 MySQL才人文結束
4、多個命令用分號隔開 create database stu;drop database stu
5、引號要打全 不然分號都不讓出
常用操作的單詞 : create(創建)、use(使用)、drop(刪除)、show(顯示)、databases(數據庫)、table(表)
一、在環境下打開MySQL console
二、基本操作
1、顯示當前所有數據庫 show databases;
2、新建ceshi數據庫 create database ceshi;
手動修改數據庫的編碼格式:alter database ceshi character set utf8;
3、使用數據庫 use ceshi;
4、顯示數據庫中的表 show table;
備注:數據類型
數字型:
整型:tinyint 1字符 2^8 1字符=256個字節
int 4字節 2^64
decimal(m,d) 定點型 m數字的總長度
d小數的位數
decimal(5,2) 1.2896-1.29
999.123-999.12
字符型:
定長字符 char(255)
變長字符 varchar(255)
日期時間 datetime 8字節 2018-06-01 09:05:01
主鍵:primary key
自增長列:auto_increment
非空:not null
默認值: default 值
5、建表
需要指明 數據類型 非空
每個表都要設置一個主鍵
create table myclass(
id int(4) not null primary key auto_increment,
name char(20) not null,
sex int(4) not null default '0',
degree decimal(16,2)
)
6、查看表結構
desc myclass;
========================================================
show = 顯示
create = 創造
database = 數據庫
7、插入數據
insert into myclass values('','張寶中','','98.5');
8、查看表中的數據
select * from myclass;
9、修改數據
update myclass set name = '張寶中' where id = 1;
10、刪除數據
delete from myclass where id = 1;
二、
alter table myclass + 語句
1、修改表名
rename as newcl;
2、添加字段
add age int(4) not null;
add pid int(4) not null first; 在第一列添加pid
add selfsm char(255) not null after age; 在age列添加selfsm
3、修改字段的名字和類型
change age newage char(20);
4、修改某一字段的類型
modify column newage int(4);
5、添加默認值-如果有默認值,先刪除再添加
alter column newage drop default;
alter column newage set default 18;
6、刪除字段
drop id;
7、添加主鍵
add primary key(字段名);
8、刪除主鍵
drop primary key;
9、刪除表
drop table newcl;
show tables;
10、刪除數據庫
drop database ceshi;
show databases;(查看所有數據庫)