1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
sqlite3 test.db
.help
.read friends.sql
.TABLE s
SELECT * FROM Friends;
.separator
.mode column .headers on
SELECT Name, Title FROM Authors NATURAL JOIN Books;
.width 1518
.show
.schema Cars
.prompt "> "". "
sqlite3 test.db "SELECT * FROM Cars;"
.output cars2.sql
.dump Cars
位置 /home/jinlong tail -5~/.sqlite_history
/home/jinlong
每次sqlite3启动时,会自动加载其中的sqlite命令
sqlite3
sqlite3 -echo -line -noheader test.db
sqlite3 -version
sqlite3 -html test.db
CREATE
CREATE TABLE Testing(Id INTEGER);
.schema Testing
CREATE TABLE Testing(Id INTEGER);
CREATE TABLE IF NOT EXISTS Testing(Id INTEGER);
CREATE TABLE Cars2 AS SELECT * FROM Cars;
.databases
ATTACH DATABASE 'test2.db' AS 'test2'; CREATE TABLE test2.Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER); INSERT INTO test2.Cars VALUES(1,'Porsche',107699); SELECT * FROM main.Cars WHERE Id=1;
CREATE TEMPORARY TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER); INSERT INTO temp.Cars VALUES(1,'Kia',24300); .databases
DROP
.TABLE s DROP TABLE Testing;
DROP TABLE IF EXISTS Testing;
DROP TABLE IF EXISTS test2.Cars;
CREATE TABLE Names(Id INTEGER, NameTEXT);
ALTER TABLE Names RENAME TO NamesOfFriends; .schema NamesOfFriends
ALTER TABLE NamesOfFriends ADD COLUMN Email TEXT; .schema NamesOfFriends
SELECT 3,'Wolf',34.5; .null value NULL
SELECT NULL;
SELECT x'345eda2348587aeb';
SELECT -(3-44);
SELECT 3 * 3/9; SELECT 3 + 4 - 1 + 5; SELECT 11 % 3;
SELECT 1 AND 1, 0 AND 1, 1 AND 0, 0 AND 0 ; SELECT 3=3 AND 4=4; SELECT 0 OR 1,1 OR 0, 1 OR 1,0 OR 1; SELECT NOT 1, NOT 0; SELECT NOT (3=3);
SELECT 3 * 3==9, 9=9; SELECT 3 <4, 3<>5, 4>=4, 5!=5;
SELECT 6 & 3; SELECT 3 & 6;
SELECT ~7;
SELECT 'Tom' IN ('Tom','Frank','Jane');
SELECT * FROM Cars WHERE Name IN('Audi','Hummer');
SELECT * FROM Cars WHERE Name LIKE'Vol%';
SELECT * FROM Cars WHERE Name LIKE'____';
SELECT * FROM Cars WHERE Name GLOB ' * en'; SELECT * FROM Cars WHERE Name GLOB '????'; AND BETWEEN SELECT * FROM Cars WHERE Price BETWEEN 20000 AND 55000;
SELECT 9/3 * 3;
DROP TABLE IF EXISTS Cars; CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER DEFAULT 'Not available'); INSERT INTO Cars(Id, Name, Price) VALUES (1,'Audi',52642); INSERT INTO Cars(Name, Price) VALUES ('Mercedes',57127); SELECT * FROM Cars;
INSERT INTO Cars VALUES (3, 'Skoda', 9000);
.nullvalue NULL
SELECT * FROM Cars WHERE Id=4;
INSERT INTO Cars VALUES(4,'Volvo',29000); INSERT OR REPLACE INTO Cars VALUES(4,'Volvo',29000); SELECT * FROM Cars WHERE Id=4;
INSERT OR FAIL INTO Cars VALUES(4,'Bentley',350000);
INSERT OR IGNORE INTO Cars VALUES(4,'Bentley',350000);
CREATE TABLE Ints(Id INTEGER PRIMARY KEY, Val INTEGER);
INSERT INTO Ints(Val) VALUES(1),(3),(5),(6),(7),(8),(6),(4),(9); CREATE TABLE Cars2(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER); INSERT INTO Cars2 SELECT * FROM Cars;
DELETE FROM Cars2 WHERE Id=1; SELECT * FROM Cars2; DELETE FROM Cars2;
SELECT Count(Id)AS'# of cars'FROM Cars2; .read cars.sql SELECT * FROM Cars; DELETE FROM Cars LIMIT5; SELECT * FROM Cars;
.read cars.sql UPDATE Cars SET Name='Skoda Octavia' WHERE Id=3; SELECT * FROM Cars WHERE Id=3;
SELECT * FROM Cars;
SELECT Name, Price FROM Cars;
SELECT Name, Price AS 'Price of car'FROM Cars;
SELECT * FROM Cars LIMIT 4;
SELECT * FROM Cars LIMIT 2, 4;
SELECT * FROM Cars LIMIT 4 OFFSET 2;
SELECT * FROM Cars ORDER BY Price; SELECT Name, Price FROM Cars ORDER BY Price DESC;
SELECT * FROM Orders WHERE Id=6; SELECT * FROM Orders WHERE Customer = "Smith"; SELECT * FROM Orders WHERE Customer LIKE 'B%';
#################################################
SELECT Customer FROM Orders WHERE Customer LIKE 'B%';
SELECT DISTINCT Customer FROM Orders WHERE Customer LIKE 'B%';
SELECT sum(OrderPrice) AS Total, Customer FROM Orders GROUP BY Customer;
SELECT sum(OrderPrice) AS Total, Customer FROM Orders GROUP BY Customer HAVING sum(OrderPrice)>1000;
CREATE TABLE People(Id INTEGER,LastName TEXT NOT NULL, FirstName TEXT NOT NULL, City TEXT);
INSERT INTO People VALUES(1,'Hanks', 'Robert', 'New York'); INSERT INTO People VALUES(2, NULL, 'Marianne', 'Chicago');
CREATE TABLE Brands(Id INTEGER, BrandName TEXT UNIQUE);
INSERT INTO Brands VALUES(1,'Coca Cola'); INSERT INTO Brands VALUES(2,'Pepsi'); INSERT INTO Brands VALUES(3,'Pepsi');
DROP TABLE Brands; CREATE TABLE Brands(Id INTEGER PRIMARY KEY,BrandName TEXT); INSERT INTO Brands(BrandName)VALUES('CocaCola'); INSERT INTO Brands(BrandName)VALUES('Pepsi'); INSERT INTO Brands(BrandName)VALUES('Sun'); INSERT INTO Brands(BrandName)VALUES('Oracle'); SELECT * FROM Brands;
FOREIGN KEY(AuthorId) REFERENCES
BEGIN TRANSACTION; DROP TABLE IF EXISTS Books; DROP TABLE IF EXISTS Authors;
CREATE TABLE Authors(AuthorId INTEGER PRIMARY KEY, Name TEXT); INSERT INTO Authors VALUES(1,'Jane Austen'); INSERT INTO Authors VALUES(2,'Leo Tolstoy'); INSERT INTO Authors VALUES(3,'Joseph Heller'); INSERT INTO Authors VALUES(4,'Charles Dickens');
CREATE TABLE Books(BookId INTEGER PRIMARY KEY, TitleTEXT, AuthorId INTEGER, FOREIGN KEY(AuthorId) REFERENCES Authors(AuthorId)); INSERT INTO Books VALUES(1,'Emma',1); INSERT INTO Books VALUES(2,'War and Peace',2); INSERT INTO Books VALUES(3,'Catch XII',3); INSERT INTO Books VALUES(4,'David Copperfield',4); INSERT INTO Books VALUES(5,'Good as Gold',3); INSERT INTO Books VALUES(6,'Anna Karenia',2); COMMIT;
PRAGMA foreign_keys=1; DELETE FROM Authors WHERE AuthorId=1;
CREATE TABLE Books(BookId INTEGER PRIMARY KEY, TitleTEXT, AuthorId INTEGER, FOREIGN KEY(AuthorId) REFERENCES Authors(AuthorId)ON DELETE CASCADE);
SELECT Name, Title FROM Authors NATURAL JOIN Books;
DELETE FROM Authors WHERE AuthorId=2; SELECT Name, Title FROM Authors NATURAL JOIN Books;
.schema Orders
INSERT INTO Orders(OrderPrice, Customer) VALUES (-10,'Johnson');
CREATE TABLE Hotels(Id INTEGER PRIMARY KEY, NameTEXT, City TEXT DEFAULT 'not available');
INSERT INTO Hotels(Name) VALUES ('Slovan');
SELECT Name, Day FROM Customers AS C JOIN Reservations AS R ON C.CustomerId = R.CustomerId;
SELECT Name, Day FROM Customers, Reservations WHERE Customers.CustomerId = Reservations.CustomerId;
SELECT Name, Day FROM Customers NATURAL JOIN Reservations;
SELECT Name, Day FROM Customers LEFT JOIN Reservations ON Customers.CustomerId = Reservations.CustomerId;
SELECT Name, Day FROM Customers LEFT JOIN Reservations USING(CustomerId);
NATURALLEFTOUTERJOIN SELECT Name, Day FROM Customers NATURAL LEFT OUTER JOIN Reservations;
SELECT sqlite_version() SELECT random() AS Random; SELECT abs(11),abs(-5),abs(0),abs(NULL); SELECT max(Price),min(Price) FROM Cars; SELECT upper (Name) AS 'Names in capitals' FROM Friends; SELECT lower (Name) AS 'Names in lowercase' FROM Friends WHERE Id IN(1,2,3); SELECT length('ZetCode'); SELECT total_changes() AS 'Total changes'; SELECT sqlite_compileoption_used('SQLITE_DEFAULT_FOREIGN_KEYS') AS 'FK'; SELECT typeof(12), typeof('ZetCode'), typeof(33.2), typeof(NULL), typeof(x'345edb');
SELECT * FROM Cars; SELECT count( * ) AS '# of cars' FROM Cars; SELECT count(Customer) AS '# of orders' FROM Orders; SELECT count(DISTINCT Customer) AS '# of customers' FROM Orders;
.nullvalue NULL CREATE TABLE TESTING(Id INTEGER); INSERT INTO Testing VALUES(1),(2),(3),(NULL),(NULL); SELECT last_insert_rowid();
SELECT count(*) AS '# of rows' FROM Testing; SELECT count(Id) AS '# of non NULL values' FROM Testing;
SELECT avg(Price) AS 'Average price' FROM Cars;
SELECT sum(OrderPrice) AS Sum FROM Orders;
SELECT date('now'); SELECT datetime('now'); SELECT time('now'); SELECT time(); SELECT date(); SELECT date('now','2 months'); SELECT date('now','-55 days'); SELECT date('now','start of year'); SELECT datetime('now','start of day'); SELECT date('now','weekday 6'); SELECT date('now','start of year','10 months','weekday 4'); SELECT strftime('%d-%m-%Y'); SELECT 'Current day: '|| strftime('%d'); SELECT 'Days to XMas: '||(strftime('%j','2015-12-24')- strftime('%j','now'));
SELECT * FROM Cars; CREATE VIEW CheapCars AS SELECT Name FROM Cars WHERE Price <30000; SELECT * FROM CheapCars; .TABLE s DROP VIEW CheapCars; .TABLE s
CREATE TABLE Log(Id INTEGER PRIMARY KEY, OldName TEXT, NewName TEXT,Date TEXT);
CREATE TRIGGER mytrigger UPDATE OF Name ON Friends
BEGIN INSERT INTO Log(OldName, NewName,Date) VALUES(old.Name,new.Name, datetime('now')); END;
SELECT name, tbl_name FROM sqlite_master WHERE type='trigger';
SELECT * FROM Friends; UPDATE FriendsSET Name='Frank' WHERE Id=3; SELECT * FROMLog;
SELECT name, tbl_name FROM sqlite_master WHERE type='trigger';
BEGIN TRANSACTION; CREATE TABLE Test(Id INTEGER NOT NULL); INSERT INTO Test VALUES(1); INSERT INTO Test VALUES(2); INSERT INTO Test VALUES(3); INSERT INTO Test VALUES(NULL); COMMIT;
BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS Test(Id INTEGER NOT NULL); INSERT INTO Test VALUES(11); INSERT INTO Test VALUES(12); INSERT INTO Test VALUES(13); INSERT INTO Test VALUES(NULL); ROLLBACK;
.mode
.import
.mode CSV .import C:/work/mydat.csv TABLE 1
.head on .mode csv .output out_Cars.csv SELECT * FROM Cars; .output stdout
.header on .separator "," .mode html .output out_Cars.html SELECT * FROM Cars; .output stdout
|