數(shù)據(jù)庫、數(shù)據(jù)表和SQL語句是什么?
數(shù)據(jù)庫是用來存儲、管理數(shù)據(jù)的倉庫
數(shù)據(jù)表是數(shù)據(jù)的存儲結(jié)構(gòu)
Structured Query Language,結(jié)構(gòu)化查詢語言,用來操作數(shù)據(jù)庫
數(shù)據(jù)庫安裝
安裝MySQL,自行百度
數(shù)據(jù)庫登錄及退出
進入cmd使用命令 mysql -hAddress -uUser -p(Password) 登錄數(shù)據(jù)庫
mysql -hlocalhost -uroot -p123456
-p后直接回車可進入密文登錄
如果出現(xiàn)以下報錯,則打開任務(wù)管理器-服務(wù)-開啟MySQL
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
開啟成功后會進入到mysql的命令行,退出輸入exit
創(chuàng)建、查看數(shù)據(jù)庫及修改名字
創(chuàng)建使用命令 create database DBName (character set Encode); 編碼不設(shè)置則為默認
create database demo;
還可加上對數(shù)據(jù)庫的編碼:
create database demo character set gbk;
查看所有數(shù)據(jù)庫
show databases;
使用命令rename database OldName to NewName修改數(shù)據(jù)庫名,有些版本可能用不了,或可進入MySQL目錄下的data修改文件名
rename database demo to newDemo;
查看及修改數(shù)據(jù)庫編碼
默認編碼為安裝時設(shè)置的編碼
show create database demo;
alter database demo character set utf8;
刪除數(shù)據(jù)庫
drop database demo;
使用或查看當前正在使用的數(shù)據(jù)庫
進入數(shù)據(jù)庫后才能進一步操作數(shù)據(jù)表
use demo;
查看當前正在使用的數(shù)據(jù)庫
select database();
創(chuàng)建、查看數(shù)據(jù)表及修改名字
創(chuàng)建表需指定字段和屬性
create table student(
_id int,
name varchar(20),
age int
);
查看所有數(shù)據(jù)表
show tables;
修改數(shù)據(jù)表名
rename table student to person;
查看及修改數(shù)據(jù)表編碼
與查看數(shù)據(jù)庫編碼同理
show create table student;
修改數(shù)據(jù)表編碼
alter table student character set utf8;
查看及修改數(shù)據(jù)表結(jié)構(gòu)
desc student;
可看到當前數(shù)據(jù)庫中各個域的屬性
使用命令alter table DBName add Field Type 增加列(字段)
alter table student add sex varchar(5);
使用命令 alter table DBName modify Field NewType(NewLength) 修改長度/類型/約束
alter table student modify name varchar(50);
使用命令 alter table DBName change OldField NewField NewType(NewLength) 修改列(字段)名
alter table student change name username varchar(50);
使用命令 alter table DBName drop Field 修改列(字段)名
alter table student drop sex;
增加約束
主鍵約束——為了保證某一個列的數(shù)據(jù)不重復
alter table student modify _id int primary key;
唯一約束——與主鍵不同的地方是可設(shè)置多個列的數(shù)據(jù)不重復
alter table student modify name varchar(50) unique;
非空約束——列必須有數(shù)據(jù)
alter table student modify name varchar(50) not null;
刪除約束
刪除主鍵約束,若是自動增長則需先取消自動增長
alter table student drop primary key;
刪除唯一約束文章來源:http://www.zghlxwxcb.cn/news/detail-707096.html
alter table student drop index name;
刪除非空約束,直接修改列為原來的定義文章來源地址http://www.zghlxwxcb.cn/news/detail-707096.html
alter table student modify name varchar(50);
刪除表
drop table student;
備份
mysqldump -hlocalhost -uroot -p demo > D:\demo.sql
恢復
mysql -uroot -p demoBak < D:\demo.sql
到了這里,關(guān)于數(shù)據(jù)庫基礎(chǔ)——數(shù)據(jù)庫、數(shù)據(jù)表和SQL語句的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!