標籤:efault 建表 date txt ip地址 資料庫 base into 插入
1.增加使用者
添加一個使用者對所有資料庫有增刪改查的功能 用命令grant(英文授權的意思)
格式為:grant 許可權 on 資料庫.* to 新增使用者@登入主機 identfied by "密碼";
例如增加使用者 paddy 密碼123456 只在本地對所有資料庫具有增刪改查的許可權
grant select,insert,update,delete on *.* to [email protected] identfied by "123456";
如果使用者要在任何主機上登入資料庫,只需要將localhost修改為"%",如果為指定的IP登入,就修改為"ip地址"
例如:grant select,insert,update,delete on *.* to [email protected]"127.0.0.1" identfied by "123456";
2.顯示、建立、刪除、選擇
show databases;
create database paddy;
drop database paddy;
use paddy;
#判斷是否存在資料庫paddy,有的話先刪除
drop database if exists paddy;
3.表 table
查表、
show tables;
#建立表
create table student(
id int auto_increment primary key,(id自增長的意思)
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
刪除表
dorp table student;
查看錶的結構
describe student; #可以簡寫為desc student;
插入資料
insert into student values(null,‘aa‘,‘男‘,‘1988-10-2‘,‘......‘);
查詢表中資料
select * from student;
查詢表中某項資料
select name from student;
修改表中某處資料
update 表名 set 列名和修改的欄位 地點
update student set nane="paddy" where id=4;
刪除資料;
delete from student where id=4;
且 and
格式 select * from 表名 where 列名 區間 and 列名 區間;
查詢出生在1980-1-10和1985-1-10之間的人
select * from student where date>‘1988-1-2‘ and date<‘1988-12-1‘;
或 or
select * from student where data>‘1988-1-01‘or date<‘1965-01-22‘;
between
select * from student where date between ‘1988-1-2‘ and ‘1988-12-1‘;
in 查詢集合內的資料
select * from student where id in {1,5,9};
表按升序排序 asc 表按降序排序 desc
格式:select * from 表名 order by asc;
select * from student order by asc;
select * from student order by desc;
查詢第n條到第M條的資料,不包含n
格式:select * from 表名 limit m,m;
select * from student limit 2,5; 顯示3-5條資料
統計表中總數 count(*)
select count(*) from student;
例如統計id總數
select count(id) from student;
修改表的名字
格式 alter table 舊的表名 rename to 新的表名;
alter table a rename to b;
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table test rename to test_rename;
#向表中增加一個欄位(列)
#格式:alter table tablename add columnname type;/alter table tablename add(columnname type);
alter table test add columnname varchar(20);
#修改表中某個欄位的名字
alter table tablename change columnname newcolumnname type; #修改一個表的欄位名
alter table test change name uname varchar(50);
select * from test;
#表position 增加列test
alter table position add(test char(10));
#表position 修改列test
alter table position modify test char(20) not null;
#表position 修改列test 預設值
alter table position alter test set default ‘system‘;
#表position 去掉test 預設值
alter table position alter test drop default;
#表position 去掉列test
alter table position drop column test;
#表depart_pos 刪除主鍵
alter table depart_pos drop primary key;
#表depart_pos 增加主鍵
alter table depart_pos add primary key PK_depart_pos
(department_id,position_id);
#用文本方式將資料裝入資料庫表中(例如D:/mysql.txt)
load data local infile "D:/mysql.txt" into table MYTABLE;
#匯入.sql檔案命令(例如D:/mysql.sql)
source d:/mysql.sql; #或者 /. d:/mysql.sql;
mysql 文法大全