您的位置:首页 > 数据库 > MySQL

MySql处理数据库和表

2015-06-07 10:02 639 查看
show databases;



mysql> show databases;





mysql> create database db_test;

Query OK, 1 row affected (0.00 sec)



use



mysql> use db_test;

Database changed





mysqldrop



mysql> drop database db_test;

Query OK, 0 rows affected (0.00 sec)




MySQL

create table

mysql> create table tb_test(
-> id int unsigned not null auto_increment,
-> firstname varchar(25) not null,
-> lastname varchar(25) not null,
-> email varchar(45) not null,
-> phone varchar(10) not null,
-> primary key(id));
Query OK, 0 rows affected (0.03 sec)


mysql> create table db_test.tb_test(
-> id int unsigned not null auto_increment,
-> firstname varchar(25) not null,
-> lastname varchar(25) not null,
-> email varchar(45) not null,
-> phone varchar(10) not null,
-> primary key(id));
Query OK, 0 rows affected (0.03 sec)


MySQLcreate table

mysql> create table if not exists db_test.tb_test(
-> id int unsigned not null auto_increment,
-> firstname varchar(25) not null,
-> lastname varchar(25) not null,
-> email varchar(45) not null,
-> phone varchar(10) not null,
-> primary key(id));
Query OK, 0 rows affected, 1 warning (0.00 sec)


Query OK

tb_testtb_test2:

mysql> create table tb_test2 select * from db_test.tb_test;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0


tb_test2

create select

mysql> describe tb_test;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(25)      | NO   |     | NULL    |                |
| lastname  | varchar(25)      | NO   |     | NULL    |                |
| email     | varchar(45)      | NO   |     | NULL    |                |
| phone     | varchar(10)      | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
mysql> create table tb_test2 select id, firstname, lastname, email from tb_test;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> describe tb_test2;
+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| id        | int(10) unsigned | NO   |     | 0       |       |
| firstname | varchar(25)      | NO   |     | NULL    |       |
| lastname  | varchar(25)      | NO   |     | NULL    |       |
| email     | varchar(45)      | NO   |     | NULL    |       |
+-----------+------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)


MySQL

temporarycreate table

mysql> create temporary table emp_temp select firstname, lastname from tb_test;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0


MySQLMySQLdrop table

show tables

mysql> show tables;
+-------------------+
| Tables_in_db_test |
+-------------------+
| tb_test           |
| tb_test2          |
+-------------------+
2 rows in set (0.00 sec)


describe

mysql> describe tb_test;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(25)      | NO   |     | NULL    |                |
| lastname  | varchar(25)      | NO   |     | NULL    |                |
| email     | varchar(45)      | NO   |     | NULL    |                |
| phone     | varchar(10)      | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


show

mysql> show columns in tb_test;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(25)      | NO   |     | NULL    |                |
| lastname  | varchar(25)      | NO   |     | NULL    |                |
| email     | varchar(45)      | NO   |     | NULL    |                |
| phone     | varchar(10)      | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


drop table

drop [temporary] table [if exists] tbl_name [, tbl_name, ...]


alter

create tablealter tabletb_demoemail

mysql> alter table tb_demo add column email varchar(45);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0


firstafterlast

emailnot null

mysql> alter table tb_demo change email email varchar(45) not null;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0


email

mysql> alter table tb_demo drop email;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: