mysql的相關操做

 

安裝mysqlmysql

安裝windows服務sql

破解密碼數據庫

統一字符編碼windows

基本的sql語句ide

- 數據庫
- show databases
- create database db1 charset gbk;
- use db1;
- drop database db1;
- alter database db1 charset utf8
- 表
- a = 2 a='2' String a = '2';
- create table t1(id int,name varchar(20) );
- alter
- drop table t1;
- select id from t1; select id,name from t1; select * from t1;
- desc t1;
- 記錄
- insert into t1(id,name) values(1,'alex');
- insert into t1 values();插入空的值
- update
- delete table t1 where id =2;編碼

今日內容索引

補充內容:建立用戶和受權ip

 

1.建立用戶:
# 指定ip:192.118.1.1的mjj用戶登陸
create user 'mjj'@'192.118.1.1' identified by '123';
# 指定ip:192.118.1.開頭的mjj用戶登陸
create user 'mjj'@'192.118.1.%' identified by '123';
# 指定任何ip的mjj用戶登陸
create user 'mjj'@'%' identified by '123';

2.刪除用戶
drop user '用戶名'@'IP地址';


3.修改用戶
rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址';

4.修改密碼
set password for '用戶名'@'IP地址'=Password('新密碼');


對當前的用戶受權管理
查看權限
show grants for '用戶'@'IP地址'

#受權 mjj用戶僅對db1.t1文件有查詢、插入和更新的操做
grant select ,insert,update on db1.t1 to "mjj"@'%';

# 表示有全部的權限,除了grant這個命令,這個命令是root纔有的。mjj用戶對db1下的t1文件有任意操做
grant all privileges on db1.t1 to "mjj"@'%';
#mjj用戶對db1數據庫中的文件執行任何操做
grant all privileges on db1.* to "mjj"@'%';
#mjj用戶對全部數據庫中文件有任何操做
grant all privileges on *.* to "mjj"@'%';

遠程鏈接:
mysql -uskx -P3306 -h 192.168.15.113 -p123ci

複製表rem

#即複製表結構 又複製記錄
create table t2 select * from db1.t1;

# 只複製表結構,不復制記錄
create table t2 select * from db1.t1 where 1>3;
create table t2 like db1.t1;

 

數據類型

整型 默認是有符號

數據類型 無符號(unsigned)和有符號 用0填充 zerofill

約束的做用: 保證數據的完整性和一直性

- tinyint [-128~127] 小整數
- int 整數
- bigint 極大整數

create table t1(id int(4) unsigned,name char(20));

---

浮點型

- float 單精度 隨着小數位數的增多,不許確
- double 雙精度 隨着小數位數的增多.不許確,比float要準確
- decimal 小數 精準的小數

 

日期類型

year 年份 (1901~2155)

date 年月日

time 時分秒

datetime 年月日 時分秒

now() sql語言中自帶的內容函: 獲取當前的時間(根據數據類型)

create table t10(born_year year,intClass datetime);

字符類型

- char 定長,簡單粗暴,浪費空間,存取速度快
- varchar 變長,精準,節省空間,存取速度慢

length():查看字節數
char_length():查看字符數

枚舉和集合

create table consumer(
id int,
name varchar(50),
sex enum('male','female','other') default 'male',
level enum('vip1','vip2','vip3','vip4'),#在指定範圍內,多選一
fav set('play','music','read','study') #在指定範圍內,多選多
);

注意:在sql中使用tinyint(1)來表示boolean類型

 

完整性約束

not null 與 default

- 若是單獨設置not null 不能插入空值
- 若是即設置了not null,又指定default,能夠插入空值,會走default

unique key

單列惟一

create table t4(
id int not null,
name char(20) unique
);

create table t4(
id int not null,
name char(20),
unique(name)
);
insert into t4(id,name) values(1,'alex');
insert into t4(id,name) values(1,'wusir');

多列惟一

- 只要有一列相同,不能插入

create table t5(
id int,
name char(20),
unique(id),
unique(name)
);

 

聯合惟一 ***

- 多列相同時,不能插入

create table t6(
id int,
name char(20),
unique(id,name)
);

應用場景: 選課系統,一個學生能夠選擇多個課程,一個課程能夠被多個學生選擇,

student_id course_name

100 '生物'

101 '生物'

100 '化學

 

 

primary key

化學反應: not null + unique

單列主鍵 不能爲空 而且是惟一

# primary key 索引(針對於大量數據) 查詢速度要快
create table t7(
id int primary key,
name varchar(10) unique
);

create table t8(
id int not null unique,
name varchar(10) unique
);

 

聯合主鍵

create table t9(
id int,
name varchar(10),
primary key(id,name)
);

 

auto_increment

create table student(
id int primary key auto_increment,
name varchar(20) not null,
sex enum('male','female') default 'male',
ip varchar(20) unique
);

insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');

*清空表區分delete和truncate的區別:*

delete from t1; #若是有自增id,新增的數據,仍然是以刪除前的最後同樣做爲起始。

truncate table t1;數據量大,刪除速度比上一條快,且直接從零開始。

foreign key ***

外鍵

 

 

# 先建立主表 create table dep( id int primary key auto_increment, name char(10) unique, dep_desc varchar(50) not null ); # 校區表 create table school( id int primary key auto_increment, addr varchar not null ) # 建立從表 create table emp( eid int primary key auto_increment, name char(10) not null, age int not null, dep_id int, school_id int, constraint fk_dep foreign key(dep_id) references dep(id) on delete cascade on update cascade, constraint fk_school foreign key(school_id) references school(id) on delete cascade on update cascade, ); insert into dep(name,dep_desc) values('校長部','校長管理有限部門'),('公關部','公關管理有限部門'),('IT部門','IT技術有限部門'),('財務部','管錢不少部門'); insert into emp(name,age,dep_id) values ('alex',18,1), ('wusir',30,2), ('吳老闆',20,3), ('馬老闆',18,4), ('邱老闆',20,2), ('女神',16,3);

相關文章
相關標籤/搜索