MySQL笔记创建表结构_mysql笔记
mysql端口號:3306
mysql默認管理員:root
進入本機mysql的命令:mysql -u 用戶名 -p密碼,例如:mysql -u root -p123
遠程訪問mysql的指令:mysql -h 主機地址 -u 用戶名 -p密碼 例如:mysql -h 172.18.101.33 -u root -p123
查看數據庫的指令:show databases;
------------------------------------創建與刪除數據庫-------------------------------------------
創建數據庫的指令: create database 數據庫名; 例如:create database wg1302a;
如果不存在則創建數據庫的指令:create database if not exists 數據庫名; 例如:create database if not exists wg1302a;
刪除數據庫的指令: drop database 數據庫名; 例如:drop database wg1302a;
如果存在則刪除數據庫的指令: drop database if exists 數據庫名; 例如:drop database if exists wg1302a;
-------------------------啟動和關閉mysql服務-------------------------------------------
第一種方法:控制面板->管理工具->服務->mysql->右鍵屬性來啟動或關閉mysql服務
第二種方法:net start mysql (開啟mysql服務的指令) 或者 net stop mysql(關閉mysql服務的指令)
打開指定的數據庫:use 數據庫名 例如:use wg1302a
顯示所有表的指令:show tables;
----------------------------創建表----------------------------------
create table student( 創建一個名為student的表
xuehao int primary key auto_increment, 學號字段,類型為整型,主鍵,自動增長
xingming varchar(15) not null, 姓名字段,類型為可變長字符,最長15個字符,不能為空
xingbie enum('m','w'), 性別字段,枚舉類型,有兩個值“w”和“m”
csrq date, 出生日期字段,日期類型
dianhua varchar(11) not null, 電話字段,可變長字符,最長11,不能為空
dizhi varchar(50), 地址字段,可變長字符,最長50
youbian char(6), 郵編字段,定長字符,長度為6
gongzi decimal(8,2) default 0.00, 工資字段,類型為浮點型,最長8位,小數點后保留2位
beizhu text 備注字段,類型為不限長字符
);
create table student(
xuehao int primary key auto_increment,
xingming varchar(15) not null,
xingbie enum('m','w'),
csrq date,
dianhua varchar(11) not null,
dizhi varchar(50),
youbian char(6),
gongzi decimal(8,2) default 0.00,
beizhu text
);
create table rgstudent(
id int primary key auto_increment,
name varchar(15),
sex enum('男','女'),
tel varchar(11),
addr varchar(50),
email varchar(20),
sal decimal(8,2)
);
create table student(
id int primary key auto_increment,
name varchar(15) not null,
sex enum('m','w'),
address varchar(50),
youbian char(6),
gongzi decimal(8,2)default 0.00,
beizhu text
);
刪除數據庫的指令:drop table 表名; 例如:drop table student;
顯示表結構的指令:desc 表名; 或者describe 表名; 例如:desc student;
--------------------------------------------修改表結構的方法-------------------------------------
alter table student add age int after birth; student表中的birth字段后增加一個age字段,類型為int
alter table student drop other; 將student表中的other字段刪除
alter table student change tel telphone varchar(11); 將student表中的tel字段的名字修改為telphone,類型為可變長11
alter table student modify sal decimal(9,2); 將student表中的sal字段的類型改為decimal(9,2)
----------------------------------復制表的指令---------------------------
create table stu like student; 創建一個與舊表student表結構相似的新表stu,注意:這種方法只復制表結構,不復制數據
insert into stu select * from student; 將student表中的所有復制到stu表中
create table stu select * from student;創建一個與舊表student表結構相似的新表stu,注意:這種方法不僅復制表結構,還復制數據
----------------數據表中的四種操作“增刪改查”,對應的指令分別為“insert into,delete,update,select ”---------------
插入數據:insert into student values (1,'zhangsan','男','1996-09-08','12345678901','beijing',8000,'010001',''); 這種方式必須按個依次插入
insert into student values (1,'zhangsan','男',null ,'12345678901','beijing',8000,'010001',null);
insert into student(name,sal) values('lisi',10000); 這種方式是選擇性的插入數據,但是必須包括所有非空字段
delete from student where id=4; 刪除student表中id為4的記錄
delete from student where id =2; 刪除student表中id為2的記錄
select * from 表名;
select * from student; 查詢student表中所有字段對應的值,其中“*”代表所有字段
update student set sex='男' where id=7; 將student表中的id為7的記錄對應的sex字段的值改為“男”
update student set name='宋旭堯' where id=88; 將id為88的記錄對應的name字段的值修改為‘宋旭堯’
-------------------------------------------表的重命名--------------------------------------------
alter table student rename [to] teacher; 將student表重新命名為teacher
alter table student rename teacher; 同上
rename table teacher to student; 將teacher表重命名為student
------------------------------------------------多種查詢語句------------------------------------
select * from student where name like '%陳%'; 查詢student表中的name字段中包含“陳”的所有記錄,其中“%”代表任意的0到多個字符
select * from student where xinzi>=8000 or sex='女'; 查詢薪資大于等于8000或者性別為女的記錄
select * from student where xinzi>=8000 and xinzi<=10000; 查詢薪資在8000到10000之間的記錄
select * from student where xinzi between 8000 and 10000; 同上
select * from student where addr in ('河南','北京'); 查詢地區在集合(北京或河南)的記錄
select addr,count(id) from student group by addr; 查詢每個地區有幾人
select addr,count(id) from student where xinzi<5000 group by addr; 查詢每個地區薪資小于5000的各有幾人
select addr,count(id) from student where xinzi>1000 group by addr order by count(id); 查詢每個地區薪資小于5000的各有幾人并排序,默認為升序
select addr,count(id) from student where xinzi>1000 group by addr order by count(id) asc;查詢每個地區薪資小于5000的各有幾人并升序排列
select addr,count(id) from student where xinzi>1000 group by addr order by count(id) desc;查詢每個地區薪資小于5000的各有幾人并降序排列
select addr,count(id) from student where xinzi>1000 group by addr order by count(id) desc limit 3; 查詢每個地區薪資小于5000的各有幾人并降序排列取前三
Select xueyuan, count(xuehao) from student where sal>=9500 group by xueyuan having count(xuehao)>3;統計student表中每個學院薪資大于9500的學生人數,并篩選出統計人數大于3的
Select xueyuan, count(xuehao) from student where sal>=9500 group by xueyuan having count(xuehao)>3 order by count(xuehao) desc limit 0,4;統計student表中每個學院薪資大于9500的學生人數,并篩選出統計人數大于3的,然后按統計人數降序排列后限定前3名
注:group by 按...進行分組
order by 按...進行排序
ascend 升序
descend 降序
----------------------------------常用函數------------------------------
select max(xinzi) from student; 求student表中薪資的最大值 max( ) 最大值函數
select min(xinzi) from student; 求student表中薪資的最小值 min( ) 最小值函數
select avg(xinzi) from student; 求student表中薪資的平均值 avg( ) 平均值函數
select sum(xinzi) from student; 求student表中薪資的總和 sum( ) 求和函數
select count(id) from student; 統計student表中id字段的總個數 count( )統計個數的函數
-------------------------------------索引的創建與刪除----------------------------
1.在建表的同時創建索引
create table xueyuan(
xyid int primary key auto_increment,
xymc enum('網工','軟工','廣告','動畫','基礎') not null,
xyrs int not null,
yz varchar(10),
index in_xymc(xymc) 為xuyuan表中的xymc字段創建名為in_xymc的索引
);
2.為已經創建好的表創建索引
create index in_name on student(name);
查看索引的指令
show index from 表名; 例如:show index from student;
show keys from 表名; 作用同上。
drop index in_name on student; 刪除student表中的名為in_name的索引
-------------------------------------擴展知識--------------------------------------------
create table jy(
id int not null,
xyid int not null,
name varchar(15),
sal decimal(8,2),
primary key (id,xyid),
foreign key (id) references student(id),
foreign key (xyid) references xueyuan(xyid)
);
------------------------------------------------修改用戶密碼的方法--------------------------------
1.update mysql.user set password=password('新密碼') where host='主機地址' and user='用戶名'; 其中“localhost”代表本地主機,“%”代表遠程主機
flush privileges; 刷新權限列表
例如:update mysql.user set password=password('123') where host='localhost' and user='root'; 將本地root用戶的密碼改為‘123’
flush privileges;
update mysql.user set password=password('123') where host='%' and user='root'; 將遠程root用戶的密碼改為‘123’
flush privileges;
2.set password for 用戶@'主機地址'=password('新密碼');注意:這種方法不需要刷新權限
例如:set password for root@'localhost'=password('789');
----------------------------忘記密碼的恢復步驟------------------------------
1.cmd
2.net stop mysql
3.mysqld --skip-grant-tables
4.再次cmd
5.mysql
6.修改密碼
7.關掉所有窗口,以新密碼進入mysql
----------------------------------------數據庫的備份與恢復------------------------------------
mysqldump -u 用戶名 -p密碼 數據庫名>盤符:\路徑\文件名.sql
例如:
1.mysqldump -u root -p123 --all-databaes>d:\wg\all.sql 備份所有數據庫到d盤wg文件夾下的all.sql文件中
2.mysqldump -u root -p123 --database wg1302a>d:\wg\wg1302a.sql 備份wg1302a數據庫到d盤wg文件夾下的wg1302a.sql文件中
3.mysqldump -u root -p123 wg1302a student>d:\wg\stu.sql 備份wg1302a數據庫中的student表到d盤wg文件夾下的stu.sql文件中
source 盤符:\路徑\文件名.sql
例如:
source d:\wg\all.sql 恢復d盤wg文件夾下的all.sql文件中備份的內容
----------------------------------------------------------用戶的創建與授權----------------------------
create user 用戶名@'主機地址' identified by'密碼';
用戶的權限可以分為四個層級:全局(標志是:“.”),數據庫(標志是:“數據庫名.*”),表(標志是:數據庫名.表名),列(標志是:權限(列名))
例子:
create user caopeng@'172.18.101.99' identified by'123';
create user liuzican@'%' identified by'123';
create user yaotian@'localhost' identified by'123';
create user wangzhiqiang@'localhost' identified by'123';
grant 權限列表 on 數據庫名.表名 to 用戶名@'主機地址';
例子:
grant all on . to caopeng@'172.18.101.99'; 其中,all代表所有權限,.代表所有數據庫的所有表
grant all on wg1302a.* to liuzican@'%';
grant select,insert,update on wg1302a.student to yaotian@'localhost';
grant select(id,name,xinzi),update(xinzi) on wg1302a.student to wangzhiqiang@'localhost';
grant 權限列表 on 數據庫名.表名 to 用戶名@'主機地址' identified by'密碼' ; 在授權的同時創建用戶
grant all on . to tom@'localhost' identified by'123';
revoke 權限列表 on 數據庫名.表名 from 用戶名@'主機地址'; 收回權限
revoke all on . from caopeng@'172.18.101.99';
-----------------------------------------------------------------------------------------------------------------------------數據庫的字符集———————————————————
mysql> show variables like 'character_set_%'; 查看數據庫的字符集
mysql> set globle character_set_server=utf8; 臨時修改字符集
vim /etc/my.cnf 在[mysql]后添加character-set-server=utf8 永久設置字符集(在linux服務器中)
vim /etc/my.ini 在[mysql]后添加default-character-set=utf8
在[mysqld]后添加character-set-server=utf8
重啟數據庫 永久設置字符集(在linux服務器中)
alter table 表名 default charset utf8; 修改表的字符集
alter database 數據庫名 character set utf-8; 修改數據庫的字符集
總結
以上是生活随笔為你收集整理的MySQL笔记创建表结构_mysql笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java创建具体时间点_java单例饿汉
- 下一篇: mysql函数及解析,Mysql研究之M