數(shù)據(jù)庫(kù)的操作筆記:加油!
跳過授權(quán)登錄:1,關(guān)閉MySQL;mysqld --skip-grant-tables
重新啟動(dòng)客戶端不用密碼就可以登錄
遠(yuǎn)程登錄數(shù)據(jù)庫(kù):
select user() 查看當(dāng)前登錄用戶
建立本機(jī)賬號(hào)
create user 'admin'@'localhost' identified by '123456';
建立遠(yuǎn)程賬號(hào)
create user 'admin'@'%' identified by '123456'; 任意主機(jī)
create user 'admin'@'192.168.20.%' identified by '123456'; 固定網(wǎng)段主機(jī)
遠(yuǎn)程登錄:mysql -h(IP) -uname -p 192.168.20.35
insert,delete,update,select
用戶授權(quán):
級(jí)別1:對(duì)所有庫(kù),下的所有表,下的所有字段
grant select on *.* to 'admin'@'%' identified by '123456';
級(jí)別2:對(duì)庫(kù)db,下的所有表,下的所有字段
grant select on db.* to 'admin'@'%' identified by '123456';
級(jí)別3;對(duì)表table,下的所有字段
grant select on db.table to 'admin'@'%' identified by '123456';
級(jí)別4: 對(duì)表table 下的字段
grant select(id,name) on db.table to 'admin'@'%' identified by '123456';
flush privileges; 刷新
1 操作文件夾(數(shù)據(jù)庫(kù)):
增:
create database database_name charset utf8;
查;
show databases;查看所有的數(shù)據(jù)庫(kù)
show create database database_name 查看database_name 創(chuàng)建信息
改:
alter database database_name charset gbk;
刪:
drop database database_name;
\c取消命令執(zhí)行
進(jìn)入文件夾操作文件(進(jìn)入庫(kù)操作表)user database_name
2 操作文件(表):
增:
create table table_name(id int,name char)engine=innodb default utf8;
查:
show tables;(查看所有表)
show create table_name;(查看創(chuàng)建表的信息)
desc table_name;(查看表結(jié)構(gòu))
改:
alter table table_name add age int;(增加字段)
alter table table_name modify name char(12);
刪:
drop table table_name;
3 操作文件的一行行內(nèi)容(記錄):
增:
insert into table_name values(1,'egon'),(2,'alex');
insert into table_name() values();
查:
select * from table_name; (查看所有)
select name,id from table_name;
改:
update table_name set name='SB' where id=4;
刪:
delete from table_name;(整體干掉)
delete from table_name where id =4;(刪除ID=4的)
#推薦truncate刪除,速度快,
delete from table_name;
truncate table_name;(干掉所有,數(shù)據(jù)量大的時(shí)候刪除速度快)
自增ID (ID遞增在上一個(gè)的基礎(chǔ)上遞增)
create table table_name(id int primary key auto_increment,name char); primary key =not null unique
復(fù)制表(所有內(nèi)容):create table new_table_name select * from table_name;
復(fù)制表(不要內(nèi)容):create table new_table_name select * from table_name where 1=2;(條件為假,內(nèi)容不拷貝)
作業(yè)一:
建庫(kù)
create database db1 charset utf8;
建表插入字段
create table student(id int primary key auto_increment,name char,sex char,age int,lesson char,clsses char);
create table teacher(id int primary key auto_increment,name char,sex char,age int,profess char,lesson char,clsses char);
create table class(id int primary key auto_increment,name char);
create table lesson(id int primary key auto_increment,name char,price int,period int);
插入數(shù)據(jù)
學(xué)生:
insert into student(name,sex,age,lesson,clsses) values
('egon1','male',18,'pyhton','six'),
('egon2','male',18,'pyhton','six'),
('egon3','male',18,'pyhton','six');
老師:
insert into teacher(name,sex,age,profess,lesson,clsses) values
('egon1','male',18,'teachering','pyhton','six'),
('egon2','male',18,'teachering','pyhton','six'),
('egon3','male',18,'teachering','pyhton','six');
班級(jí):
insert into class(name) values
('egon1'),
('egon2'),
('egon3');
課程:
insert into lesson(name,price,period)values
('pyhton0',18000,6),
('pyhton1',18000,6),
('pyhton2',18000,6);
作業(yè)二:
創(chuàng)建用戶lili,只是開放lili對(duì)學(xué)生表的查詢(select)與修改(update)權(quán)限
grant select,update on db1.student to 'lili'@'%' identified by '123456';
flush privileges;
創(chuàng)建用戶Jack,只開房Jack對(duì)老師表的查詢權(quán)限
grant select on db1.teacher to 'jack'@'%' identified by '123456';
flush privileges;
創(chuàng)建用戶Tom,只允許Tom查詢和修改課程表的名字和周期
grant select(name,period),update(name,period) on db1.lesson to 'tom'@'%' identified by '123456';
flush privileges;
文章題目:數(shù)據(jù)庫(kù)的基本操作
當(dāng)前路徑:
http://weahome.cn/article/ishscj.html