Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version:5.5.53 MySQL Community Server (GPL)Copyright (c)2000,2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
<選擇(使用)一個(gè)數(shù)據(jù)庫(kù)> use 數(shù)據(jù)庫(kù)名稱; ;一定不要忘記加上
mysql> use bss;//使用這個(gè)名為bss的數(shù)據(jù)庫(kù)
Database changed
<查數(shù)據(jù)庫(kù)> show databases ;(;是語(yǔ)句的結(jié)束符,一定要加上)
mysql> show databases;//這個(gè)語(yǔ)句可以查出所以你創(chuàng)建的數(shù)據(jù)庫(kù)+--------------------+| Database |+--------------------+| information_schema || bss || myhtml || mysql || performance_schema || score || test |+--------------------+7 rows in set (0.00 sec)
<查表> show tables;(注意因?yàn)槭遣楸硭砸欢ㄒ紫冗x擇一個(gè)數(shù)據(jù)庫(kù)才能查看)
mysql> show tables;//在這里我選擇的數(shù)據(jù)庫(kù)是bss+---------------+| Tables_in_bss |+---------------+| user || zzh |+---------------+2 rows in set (0.00 sec)
<建立/刪除數(shù)據(jù)庫(kù)> create database 數(shù)據(jù)庫(kù)名稱;建立數(shù)據(jù)庫(kù) drop database 數(shù)據(jù)庫(kù)名稱;刪除數(shù)據(jù)庫(kù)
mysql> create database xiao;
Query OK,1 row affected (0.00 sec)//這樣就新建一個(gè)數(shù)據(jù)庫(kù),我們也可以在查看一下。
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || bss || myhtml || mysql || performance_schema || score || test || xiao |+--------------------+8 rows in set (0.00 sec)//這樣數(shù)據(jù)庫(kù)中便有了這個(gè)xiao數(shù)據(jù)庫(kù),接下來(lái)再刪除它。
mysql> drop database xiao;
Query OK,0 rows affected (1.85 sec)//這樣xiao數(shù)據(jù)庫(kù)就被刪除了。
<創(chuàng)建/刪除一個(gè)表> create table 表名 ( id int ,name varchar(35),password varchar(40)); 其中 id name password 是字段 ,后面的限制是類型,括號(hào)里的字段可以自己進(jìn)行設(shè)置,只要格式對(duì)就可以。 drop table 表名; 注意在創(chuàng)建數(shù)據(jù)表時(shí)一定要先選擇一個(gè)數(shù)據(jù)庫(kù)。
mysql> create table hello(id int,you varchar(30),me varchar(30));
Query OK,0 rows affected (1.78 sec)//接下來(lái)我們查看一下
mysql> show tables;+---------------+| Tables_in_bss |+---------------+| hello || user || zzh |+---------------+3 rows in set (0.00 sec)//這樣bbs數(shù)據(jù)庫(kù)中便有這個(gè)hello的數(shù)據(jù)表了
mysql> drop table hello;
Query OK,0 rows affected (0.00 sec)
mysql> show tables;+---------------+| Tables_in_bss |+---------------+| user || zzh |+---------------+2 rows in set (0.00 sec)//這樣就刪除了剛才所創(chuàng)建的數(shù)據(jù)表
<查看表結(jié)構(gòu)> desc 表名;
mysql> desc user;//查看表結(jié)構(gòu)+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id |int(11)| YES ||NULL||| pass |varchar(30)| YES ||NULL||| name |varchar(30)| YES ||NULL||| ttt |varchar(40)| YES ||NULL||| sss |varchar(20)| YES ||NULL||+-------+-------------+------+-----+---------+-------+5 rows in set (0.01 sec)
<查看建表/建庫(kù)語(yǔ)句> show create table 表名稱; show create database 數(shù)據(jù)庫(kù)名稱;
mysql> show create table user;//查看建表命令+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| user | CREATE TABLE `user` (`id` int(11) DEFAULT NULL,`pass` varchar(30) DEFAULT NULL,`name` varchar(30) DEFAULT NULL,`ttt` varchar(40) DEFAULT NULL,`sss` varchar(20) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8 |+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
mysql> show create database bss;//查看建庫(kù)語(yǔ)句+----------+--------------------------------------------------------------+| Database | Create Database |+----------+--------------------------------------------------------------+| bss | CREATE DATABASE `bss` /*!40100 DEFAULT CHARACTER SET utf8 */|+----------+--------------------------------------------------------------+1 row in set (0.00 sec)
<添加/刪除表字段> alter table 表名 add 字段名稱+字段類型; alter table 表名 drop 字段名字;
mysql> show tables;+------------------+| Tables_in_myhtml |+------------------+| user |+------------------+1 row in set (0.00 sec)mysql> desc user;+----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+----------------+| Id |int(11)| NO | PRI |NULL| auto_increment || username |varchar(255)| YES ||NULL||| password |varchar(255)| YES ||NULL||+----------+--------------+------+-----+---------+----------------+3 rows in set (1.67 sec)//現(xiàn)在我們?cè)黾右粋€(gè)字段名為passmysql> alter table user add pass varchar(30);
Query OK,3 rows affected (0.56 sec)
Records:3 Duplicates:0 Warnings:0mysql> desc user;//進(jìn)行查詢+----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+----------------+| Id |int(11)| NO | PRI |NULL| auto_increment || username |varchar(255)| YES ||NULL||| password |varchar(255)| YES ||NULL||| pass |varchar(30)| YES ||NULL||+----------+--------------+------+-----+---------+----------------+4 rows in set (0.01 sec)//這樣的話就有一個(gè)新的字段pass,可以看出剛才確實(shí)新建了一個(gè)字段,接下來(lái)刪除它。
mysql> alter table user drop pass;
Query OK,3 rows affected (1.81 sec)
Records:3 Duplicates:0 Warnings:0mysql> desc user;//再次查詢+----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+----------------+| Id |int(11)| NO | PRI |NULL| auto_increment || username |varchar(255)| YES ||NULL||| password |varchar(255)| YES ||NULL||+----------+--------------+------+-----+---------+----------------+3 rows in set (0.01 sec)//這樣的話就將新建的pass字段給刪除了
<插入順序的問題> first alter table 表名 add 字段名稱+類型 first; after alter table 表名 add 字段名稱+類型 after+字段名稱;
mysql> desc user;//先查一下user數(shù)據(jù)表的結(jié)構(gòu)+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id |int(11)| YES ||NULL||| pass |varchar(30)| YES ||NULL||| name |varchar(30)| YES ||NULL||| ttt |varchar(40)| YES ||NULL||| sss |varchar(20)| YES ||NULL||+-------+-------------+------+-----+---------+-------+5 rows in set (0.01 sec)//然后我們想創(chuàng)建一個(gè)字段并且想讓它排到第一位
mysql> alter table user add kkk varchar(34) first;
Query OK,0 rows affected (1.84 sec)
Records:0 Duplicates:0 Warnings:0//經(jīng)過上面的命令,小伙伴們可以對(duì)比一下
mysql> desc user;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| kkk |varchar(34)| YES ||NULL||| id |int(11)| YES ||NULL||| pass |varchar(30)| YES ||NULL||| name |varchar(30)| YES ||NULL||| ttt |varchar(40)| YES ||NULL||| sss |varchar(20)| YES ||NULL||+-------+-------------+------+-----+---------+-------+6 rows in set (0.01 sec)//想將建立的字段放到某一字段后,原理也是一樣,小伙伴們可以按照那個(gè)命令去嘗試下
<修改表的名字> alter table 原表名 rename 新的名字;
mysql> use bss;
Database changed
mysql> show tables;+---------------+| Tables_in_bss |+---------------+| user || zzh |+---------------+2 rows in set (0.00 sec)//接下來(lái)我們把數(shù)據(jù)表user的名字改為username
mysql> alter table user rename username;
Query OK,0 rows affected (1.75 sec)//這些便實(shí)現(xiàn)了改表名稱
mysql> show tables;+---------------+| Tables_in_bss |+---------------+| username || zzh |+---------------+2 rows in set (0.00 sec)
<修改表中的字段名稱> alter table 表名 change 原來(lái)的字段名稱 修改后的字段名稱+類型;
mysql> desc zzh;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id |int(11)| YES ||NULL||| he |varchar(20)| YES ||NULL||| me |varchar(40)| YES ||NULL||+-------+-------------+------+-----+---------+-------+3 rows in set (0.01 sec)//這里我們想把字段he改為you
mysql> alter table zzh change he you varchar(20);
Query OK,0 rows affected (0.37 sec)
Records:0 Duplicates:0 Warnings:0//再次進(jìn)行查詢
mysql> desc zzh;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id |int(11)| YES ||NULL||| you |varchar(20)| YES ||NULL||| me |varchar(40)| YES ||NULL||+-------+-------------+------+-----+---------+-------+3 rows in set (0.01 sec)//可以看到he已經(jīng)改為了you