mysql建表常用的sql語句匯總
最近跟項目,寫后臺需要用到SQL語句,就整理了一下mysql建表常用sql語句,并寫幾個可執(zhí)行SQL腳本,方便日后復(fù)習(xí)查看以及使用:
連接:mysql -h主機地址 -u用戶名 -p用戶密碼 (注:u與root可以不用加空格,其它也一樣)
斷開:exit (回車)
創(chuàng)建授權(quán):grant select on 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by \"密碼\"
修改密碼:mysqladmin -u用戶名 -p舊密碼 password 新密碼
刪除授權(quán): revoke select,insert,update,delete om *.* from test2@localhost;
顯示數(shù)據(jù)庫:show databases;
顯示數(shù)據(jù)表:show tables;
顯示表結(jié)構(gòu):describe 表名;
創(chuàng)建庫:create database 庫名;
刪除庫:drop database 庫名;
使用庫(選中庫):use 庫名;
創(chuàng)建表:create table 表名 (字段設(shè)定列表);
刪除表:drop table 表名;
修改表:alter table t1 rename t2
查詢表:select * from 表名;
清空表:delete from 表名;
備份表: mysqlbinmysqldump -h(ip) -uroot -p(password) databasename tablename > tablename.sql
恢復(fù)表: mysqlbinmysql -h(ip) -uroot -p(password) databasename tablename < tablename.sql
(操作前先把原來表刪除)
增加列:ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,ADD INDEX (c);
修改列:ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
刪除列:ALTER TABLE t2 DROP COLUMN c;
備份數(shù)據(jù)庫:mysql\bin\mysqldump -h(ip) -uroot -p(password) databasename > database.sql
恢復(fù)數(shù)據(jù)庫:mysql\bin\mysql -h(ip) -uroot -p(password) databasename < database.sql
復(fù)制數(shù)據(jù)庫:mysql\bin\mysqldump --all-databases > all-databases.sql
修復(fù)數(shù)據(jù)庫:mysqlcheck -A -o -uroot -p54safer
文本數(shù)據(jù)導(dǎo)入: load data local infile \"文件名\" into table 表名;
數(shù)據(jù)導(dǎo)入導(dǎo)出:mysql\bin\mysqlimport database tables.txt
以下為MySQL的可執(zhí)行腳本示例:
1.創(chuàng)建用戶表示例
//創(chuàng)建用戶表示例 /* Navicat MySQL Data Transfer Source Server : localhost_1111 Source Server Version : 50717 Source Host : localhost:1111 Source Database : maven Target Server Type : MYSQL Target Server Version : 50717 File Encoding : 65001 Date: 2018-08-15 22:40:44 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `pk_id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `password` char(32) NOT NULL, `age` int(3) DEFAULT NULL, `info` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL, `createtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, `modifytime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, `sex` char(1) DEFAULT NULL, PRIMARY KEY (`pk_id`) ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFA
2.創(chuàng)建公司網(wǎng)站主頁欄目示例
//某公司網(wǎng)站主頁欄目示例 SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for home -- ---------------------------- DROP TABLE IF EXISTS `home`; CREATE TABLE `home` ( `home_id` int(10) NOT NULL AUTO_INCREMENT, `profile` longtext comment '企業(yè)簡介', `scope` longtext comment '經(jīng)營范圍', `product` longtext comment '產(chǎn)品介紹', `cooperate` longtext comment '校企合作', `extension` longtext comment '其他', PRIMARY KEY (`home_id`) ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8;
3.創(chuàng)建店鋪商品實例
//某電商平臺店鋪商品實例 create database store; #創(chuàng)建數(shù)據(jù)庫store use store; set names utf8; drop table if exists goods; create table goods ( id mediumint unsigned not null auto_increment comment 'Id', goods_name varchar(150) not null comment '商品名稱', market_price decimal(10,2) not null comment '市場價格', shop_price decimal(10,2) not null comment '本店價格', goods_desc longtext comment '商品描述', is_on_sale enum('是','否') not null default '是' comment '是否上架', is_delete enum('是','否') not null default '否' comment '是否放到回收站', addtime datetime not null comment '添加時間', logo varchar(150) not null default '' comment '原圖', sm_logo varchar(150) not null default '' comment '小圖', mid_logo varchar(150) not null default '' comment '中圖', big_logo varchar(150) not null default '' comment '大圖', mbig_logo varchar(150) not null default '' comment '更大圖', primary key (id), key shop_price(shop_price), key addtime(addtime), key is_on_sale(is_on_sale) )engine=InnoDB default charset=utf8 comment '商品'; drop table if exists brand; create table brand ( id mediumint unsigned not null auto_increment comment 'Id', brand_name varchar(30) not null comment '品牌名稱', site_url varchar(150) not null default '' comment '官方網(wǎng)址', logo varchar(150) not null default '' comment '品牌Logo圖片', primary key (id) )engine=InnoDB default charset=utf8 comment '品牌';
4.餐廳點餐菜單示例
//餐廳點餐菜單示例 /* SQLyog 企業(yè)版 - MySQL GUI v8.14 MySQL - 5.5.27 : Database - db_food ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_food` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `db_food`; /*Table structure for table `goods` */ DROP TABLE IF EXISTS `goods`; CREATE TABLE `goods` ( `id` int(10) NOT NULL AUTO_INCREMENT, `goodsName` varchar(100) DEFAULT NULL, `price` float DEFAULT NULL, `goodsDesc` varchar(200) DEFAULT NULL, `imageLink` varchar(500) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; /*Data for the table `goods` */ insert into `goods`(`id`,`goodsName`,`price`,`goodsDesc`,`imageLink`) values (3,'宮保雞丁',21,'宮保雞丁哦','D:\\我的文檔\\Desktop\\food\\1332059684_58.jpg'),(5,'青椒肉絲',22,'青椒肉絲不好吃','D:\\我的文檔\\Desktop\\food\\1332059684_58.jpg'),(8,'21',2,'sd cd','D:\\我的文檔\\Desktop\\food\\1332059994_53.jpg'),(9,'魚香肉絲',9,'四川風(fēng)味','D:\\我的文檔\\Desktop\\food\\1332060047_92.jpg'),(10,'回鍋肉',12,NULL,NULL),(11,'熱狗腸',32,'說的','D:\\我的文檔\\Desktop\\food\\1332060176_81.jpg'); /*Table structure for table `order_goods` */ DROP TABLE IF EXISTS `order_goods`; CREATE TABLE `order_goods` ( `id` int(10) NOT NULL AUTO_INCREMENT, `orderId` varchar(50) DEFAULT NULL, `goodsTotalPrice` float DEFAULT NULL, `goodsId` int(10) DEFAULT NULL, `goodsPrice` float DEFAULT NULL, `goodsNum` int(10) DEFAULT NULL, `goodsName` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_order_goods_2` (`orderId`), KEY `FK_order_goods_1` (`goodsId`), CONSTRAINT `FK_order_goods_1` FOREIGN KEY (`goodsId`) REFERENCES `goods` (`id`), CONSTRAINT `FK_order_goods_2` FOREIGN KEY (`orderId`) REFERENCES `order_info` (`orderId`) ) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8; /*Data for the table `order_goods` */ insert into `order_goods`(`id`,`orderId`,`goodsTotalPrice`,`goodsId`,`goodsPrice`,`goodsNum`,`goodsName`) values (23,'20130708001514',12,10,12,1,'回鍋肉'),(28,'20130708021437',12,10,12,1,'回鍋肉'),(31,'20130708110510',22,5,22,1,'青椒肉絲'),(32,'20130708110510',9,9,9,1,'魚香肉絲'),(33,'20130708110513',12,10,12,1,'回鍋肉'),(34,'20130708110513',32,11,32,1,'熱狗腸'),(39,'20130708115503',2,8,2,1,'21'),(40,'20130708115508',12,10,12,1,'回鍋肉'),(41,'20130708115508',32,11,32,1,'熱狗腸'),(42,'20130708115512',22,5,22,1,'青椒肉絲'),(43,'20130708121456',9,9,9,1,'魚香肉絲'); /*Table structure for table `order_info` */ DROP TABLE IF EXISTS `order_info`; CREATE TABLE `order_info` ( `orderId` varchar(50) NOT NULL, `orderStatus` int(10) DEFAULT NULL, `orderNum` int(10) DEFAULT NULL, `orderTotalMoney` float DEFAULT NULL, `userName` varchar(100) DEFAULT NULL, PRIMARY KEY (`orderId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `order_info` */ insert into `order_info`(`orderId`,`orderStatus`,`orderNum`,`orderTotalMoney`,`userName`) values ('20130708001514',3,1,12,'admin1'),('20130708021437',1,1,12,'admin1'),('20130708110510',2,2,31,'aaa'),('20130708110513',3,2,44,'aaa'),('20130708115503',1,1,2,'admin1'),('20130708115508',4,2,44,'admin1'),('20130708115512',3,1,22,'admin1'),('20130708121456',4,1,9,'admin1'); /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `userName` varchar(100) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `email` varchar(200) DEFAULT NULL, `rank` int(1) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; /*Data for the table `user` */ insert into `user`(`id`,`userName`,`password`,`email`,`rank`) values (1,'admin','123',NULL,1),(8,'aaa','123','ad@1.com',0),(9,'admin1','123',NULL,0); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
到此這篇關(guān)于mysql建表常用sql語句的文章就介紹到這了,更多相關(guān)mysql建表sql語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教你快速生成MySQL數(shù)據(jù)庫關(guān)系圖
我們經(jīng)常會用到一些表的數(shù)據(jù)庫關(guān)系圖,下面這篇文章主要給大家介紹了關(guān)于生成MySQL數(shù)據(jù)庫關(guān)系圖的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06淺談sql語句中GROUP BY 和 HAVING的使用方法
GROUP BY語句和HAVING語句,經(jīng)過研究和練習(xí),終于明白如何使用了,在此記錄一下同時添加了一個自己舉的小例子,通過寫這篇文章來加深下自己學(xué)習(xí)的效果,還能和大家分享下,同時也方便以后查閱,一舉多得,下面由小編來和大家一起學(xué)習(xí)2019-05-05Mysql數(shù)據(jù)庫中把varchar類型轉(zhuǎn)化為int類型的方法
這篇文章主要介紹了Mysql數(shù)據(jù)庫中把varchar類型轉(zhuǎn)化為int類型的方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07mysql 從 frm 文件恢復(fù) table 表結(jié)構(gòu)的3種方法【推薦】
這篇文章主要介紹了mysql 從 frm 文件恢復(fù) table 表結(jié)構(gòu)的3種方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09