安裝指南:css
https://www.cnblogs.com/majj/p/9160383.html 小馬哥html
下載完後初始化操做數據庫:mysql
1. 將文件放在 :sql
G:\軟件\mysql-8.0.15-winx64\bin數據庫
2. 執行初始化命令spa
進入 bin目錄下 進行初始化code
mysqld --initialize-insecure --user=mysql htm
3.啓動mysqlblog
net start mysql索引
4. 若是step3 失敗的話,執行mysqld -install 命令 ,這個命令須要在admin管理
mysqld -install
5. mysql 8版本可能須要root密碼,咱們重置一下密碼:
net stop mysql
mysqld --console --skip-grant-tables --shared-memory
mysql.exe -u root
UPDATE mysql.user SET authentication_string='root' WHERE user='root' and host='localhost';
1. 建立數據庫 mysql> create database maizi1; Query OK, 1 row affected (0.01 sec) mysql> create database if not exists maizi default character set "utf8"; Query OK, 1 row affected (0.00 sec) mysql> use maizi; Database changed mysql> 2. 建立數據庫表 user create table if not exists user( id smallint, username varchar(20), age tinyint, sex enum("男","女","保密"), email varchar(50), addr varchar(200), birth YEAR, salary float(8,2), tel int, married tinyint(1) comment "0,表明未婚.非零表明已經結婚." )engine =innodb charset =utf8; 3. 建立 數據庫表 course create table if not exists course( cid tinyint, couseName varchar(50), courseDesc varchar(200) ); 4. 建立表cms create table if not exists cms_cate( id tinyint, cateName varchar(50), cateDesc varchar(200) )engine=myisam charset =utf8; 5. 建立表cms_news create table if not exists cms_news( id int, title varchar(50), content text, pubTime int, clickNum int, isTop tinyint(1) comment "0表明不置頂,1表明置頂." );
6. 查看錶結構 mysql> describe user; +----------+------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------------+------+-----+---------+-------+ | id | smallint(6) | YES | | NULL | | | username | varchar(20) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | | sex | enum('男','女','保密') | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | addr | varchar(200) | YES | | NULL | | | birth | year(4) | YES | | NULL | | | salary | float(8,2) | YES | | NULL | | | tel | int(11) | YES | | NULL | | | married | tinyint(1) | YES | | NULL | | +----------+------------------------+------+-----+---------+-------+ mysql> show columns from user; +----------+------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------------+------+-----+---------+-------+ | id | smallint(6) | YES | | NULL | | | username | varchar(20) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | | sex | enum('男','女','保密') | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | addr | varchar(200) | YES | | NULL | | | birth | year(4) | YES | | NULL | | | salary | float(8,2) | YES | | NULL | | | tel | int(11) | YES | | NULL | | | married | tinyint(1) | YES | | NULL | | +----------+------------------------+------+-----+---------+-------+ 10 rows in set (0.00 sec)
7. 建立主鍵.(能夠省掉primary 關鍵字.) mysql> create table if not exists user1( -> id int primary key, -> username varchar(20) -> ); Query OK, 0 rows affected, 1 warning (0.00 sec) 查看錶. mysql> desc user1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | username | varchar(20) | YES | | NULL | | +----------+ -------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> 8.設置複合主鍵. mysql> create table if not exists user2( -> id int, -> username varchar(20), -> card char(18), -> primary key(id,card) -> ); Query OK, 0 rows affected (0.04 sec) 9. 自增加. auto_increment mysql> create table if not exists user10( -> id smallint key auto_increment, -> username varchar(20) -> ); Query OK, 0 rows affected (0.04 sec) mysql> desc user10; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | smallint(6) | NO | PRI | NULL | auto_increment | | username | varchar(20) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
10. 非空. NOT NULL. mysql> create table if not exists user7( -> id int unsigned key auto_increment, -> username varchar(20) not null, -> password char(32) not null, -> age tinyint unsigned -> ); Query OK, 0 rows affected (0.06 sec) mysql> desc user7; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> insert user7(username,password) values("king","king") -> ; Query OK, 1 row affected (0.00 sec) 11. DEFAULT 默認值. mysql> create table if not exists user8( -> id int unsigned key auto_increment, -> username varchar(20) not null, -> password char(32) not null, -> age tinyint unsigned default 19, -> addr varchar(50) not null default "北京" -> ); Query OK, 0 rows affected (0.04 sec) mysql> desc user8 -> ; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 19 | | | addr | varchar(50) | NO | | 北京 | | +----------+---------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) 12. 惟一約束.unique mysql> create table if not exists user11( -> id tinyint unsigned key auto_increment, -> username varchar(20) not null unique , -> card char(18) unique -> ); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> desc user11; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | card | char(18) | YES | UNI | NULL | | +----------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> insert user11(username) value("a"); Query OK, 1 row affected (0.01 sec) mysql> select * from user11; +----+----------+------+ | id | username | card | +----+----------+------+ | 1 | a | NULL | +----+----------+------+ 1 row in set (0.00 sec)
13. 重命名錶名. mysql> create table user13( -> id smallint unsigned key auto_increment, -> username varchar(20)not null unique, -> password char(32) not null, -> email varchar(50) not null default "393376780@qq.com", -> age tinyint unsigned default 18, -> addr varchar(200) not null default " 北京", -> salary float(6,2), -> regTime int unsigned, -> face char(100) not null default "default.jpg" -> ); Query OK, 0 rows affected (0.03 sec) mysql> desc user13; +----------+----------------------+------+-----+------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+------------------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(50) | NO | | 393376780@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(200) | NO | | 北京 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | +----------+----------------------+------+-----+------------------+----------------+ 9 rows in set (0.00 sec) mysql> alter table user13 rename to user31; Query OK, 0 rows affected (0.01 sec) mysql> rename table user31 to user13; Query OK, 0 rows affected (0.01 sec) 14. 添加字段. mysql> alter table user13 add card char(18); Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0 #在username字段後面添加字段. mysql> alter table user13 add test10 int not null default 100 after username; Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0 #在第一個字段位置添加. mysql> alter table user13 add test100 int not null default 100 first; Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0 15. 刪除字段. mysql> alter table user13 drop test10; Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 16. 修改字段屬性 mysql> alter table user13 modify card varchar(200); Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table user13 change card cardchange char(44); Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user13; +------------+----------------------+------+-----+------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+----------------------+------+-----+------------------+----------------+ | test100 | int(11) | NO | | 100 | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(50) | NO | | 393376780@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(200) | NO | | 北京 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | cardchange | char(44) | YES | | NULL | | +------------+----------------------+------+-----+------------------+----------------+ 11 rows in set (0.00 sec)
1. 複合索引. mysql> alter table user1 add primary key(id,username); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | username | varchar(20) | NO | PRI | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) 2. 刪除主鍵 mysql> alter table user1 drop primary key; Query OK, 0 rows affected (0.52 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | username | varchar(20) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) 3. 惟一索引. mysql> alter table user1 add unique(username); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | username | varchar(20) | NO | PRI | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> alter table user12 add constraint symbol unique key uni_card(face); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user12; +----------+----------------------+------+-----+------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+------------------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(50) | NO | | 393376780@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(200) | NO | | 北京 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | UNI | default.jpg | | +----------+----------------------+------+-----+------------------+----------------+ 9 rows in set (0.00 sec) 修改默認引擎. mysql> alter table user1 ENGINE=MyISAM; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0