View existing databases:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.05 sec)
New database statement:
mysql> create database wzu; Query OK, 1 row affected (0.05 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | | wzu | +--------------------+ 3 rows in set (0.05 sec)
To operate on a database, first select the database and use use <dbname>:
mysql> use wzu
Database changed
Create the form:
Command: create table <table name> ( <attribute name attribute type [modification]> );
create table student(sno char(9) primary key,sname char(20) unique,ssex char(2),sage smallint, sdept char(20)); create table course(cno char(4) primary key,cname char(40) not null,cpno char(4),ccredit smallint,foreign key(cpno) references course(cno)); create table sc(sno char(9),cno char(4),grade smallint,primary key(sno,cno),foreign key(sno) references student(sno),foreign key(cno) references course(cno));
Delete form:
Command: drop table <table name>;
mysql> drop table emp;
Query OK, 0 rows affected (0.41 sec)
See what tables are in the database:
mysql> show tables;
+---------------+
| Tables_in_wzu |
+---------------+
| course |
| sc |
| student |
+---------------+
Change foreign key constraints:
1 set foreign_key_checks= 0 ; // Ignore foreign key check 2 set foreign_key_checks= 1 ; // Restart foreign key check
Change the data type of an attribute in a table:
Command: alter table <table name> modify <attribute to be changed> <type to be changed>; for
example: change the sno field in the student table to varchar(11)
mysql> alter table student modify sno varchar(11); Query OK, 0 rows affected (0.15 sec) Records: 0 Duplicates: 0 Warnings: 0
The table structure can be viewed with desc <table name>:
mysql> desc student; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | sno | varchar ( 11 ) NO | PRI | | | | sname | varchar(20) | YES | UNI | NULL | | | ssex | varchar(2) | YES | | NULL | | | sage | smallint(6) | YES | | NULL | | | sdept | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
Table insert data:
Command: insert into <table name> values (fill, enter, correspond, correspond, the, value);
1 insert into student values( ' 1024 ' , ' test ' , ' male ' , ' 18 ' , ' CS ' );
Delete a row of data:
Command: delete from <table name> where [condition];
1 mysql> delete from emp where empno='232';
Simple data query in table:
select * from [table name]
such as: select * from student;
* means to query all the columns in the table, you can also use the column name plus ',' to separate the data you need
mysql> select * from student; +-------------+--------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-------------+--------+------+------+-------+ | 1024 | Test | Male | 18 | CS | | 1621116 | Zhou Shuai | Male | 21 | CS | +-------------+--------+------+------+-------+ mysql> select sno,sname from student; +-------------+--------+ | sno | sname | +-------------+--------+ | 1024 | Tests | | 1621116 | Zhou Shuai| +-------------+--------+
Add a column attribute to the created table:
alter table <table name> add <attribute name> <attribute domain>;
such as:
mysql> alter table dept add type varchar(11); Query OK, 4 rows affected (0.62 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc dept; +--------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+-------+ | deptno | decimal(2,0) | NO | PRI | NULL | | | dname | varchar(14) | NO | | NULL | | | loc | varchar(13) | YES | | NULL | | | type | varchar(11) | YES | | NULL | | +--------+--------------+------+-----+---------+-------+
Delete a column attribute in the created table:
alter table <table name> drop <attribute name>;
such as:
mysql> alter table dept drop type; Query OK, 4 rows affected (0.57 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc dept; +--------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+-------+ | deptno | decimal(2,0) | NO | PRI | NULL | | | dname | varchar(14) | NO | | NULL | | | loc | varchar(13) | YES | | NULL | | +--------+--------------+------+-----+---------+-------+
When a warning is found for an operation, you can view the warning information by show warnings;:
mysql> show warnings; +---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 1366 | Incorrect string value: '\xE6\x9D\x8E\xE5\x8B\x87' for column 'sname' at row 1 | | Warning | 1366 | Incorrect string value: '\xE7\x94\xB7' for column 'ssex' at row 1 | | Warning | 1366 | Incorrect string value: '\xE6\x95\xB0\xE6\x8D\xAE...' for column 'cname' at row 1 | +---------+------+-----------------------------------------------------------------------------------+
mysql view the currently selected database:
mysql> select database(); +------------+ | database() | +------------+ | groupdb | +------------+
//or:
mysql> show tables;
+-------------------+
| Tables_in_groupdb |//This is the current database
+------------- ------+ |
course |
| dept |
| emp |
|
jwc | | salgrade |
| sc |
| student |
|
-+
//or:
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.14, for Win64 (x86_64)
Connection id: 2721
Current database: groupdb //此处为当前数据库
Current user: [email protected]
SSL: Not in use
Using delimiter: ;
Server version: 5.5.56-MariaDB MariaDB Server
Protocol version: 10
Connection: AAA.AAA.AAA.AAA via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 2 days 5 hours 25 min 51 sec
Threads: 2 Questions: 549 Slow queries: 0 Opens: 26 Flush tables: 2 Open tables: 44 Queries per second avg: 0.002