I wrote a piece of sqlite3 query as following:
CREATE TABLE appearances(
char_id int,
comic_id int
);
CREATE VIEW co-actors AS
SELECT a1.char_id,
a2.char_id
FROM appearances AS a1
LEFT JOIN appearances AS a2 ON a1.comic_id=a2.comic_id;
And it keeps showing up syntax error near '_' in the command shell.
Can anybody help me correct the query? Thanks~
One more question, if I want to select from the View I just created, how do I reference the column,like a1.char_id?
Change co-actors to co_actors
You are using a - where you should be using a _.
Related
I am a completely new to SQL and I am follow a tutorial verbatim to try and create a new table in my first database. However I am getting the following error.
USE menu;
CREATE TABLE Burgers
(
`Burger Number` TINYINT,
Burger VARCHAR(50),
Price DECIMAL(5,2),
Description VARCHAR(300),
);
Yields SQL Error 1064:
SQL Error (1064): You have an errror in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 8
I made sure to use commas to separate each column and used parenthesis to what I understand is the proper way to do so. I am not sure if version is relevant but I am using version 10.5.8 of MariaDB. Any insight is appreciated!
You have one comma too many.
...
Description VARCHAR(300),
);
You need commas between each column, index, or constraint within the CREATE TABLE statement, except for the last one before the closing parenthesis.
It should be like this:
...
Description VARCHAR(300)
);
General tip about syntax errors: They tell you exactly where to look for the problem, because it reports the place in your SQL statement where the syntax parser got confused.
In this case it reported:
...right syntax to use near ')' at line 8
This tells you the problem is at that point in the syntax. The ) didn't belong there, because the syntax was expecting something else. Because commas separate columns, it was expecting another column definition following a comma.
I achieved to display a list of table names of interest I have in my database with the following function:
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name LIKE '%#_1' ESCAPE '#';
(It is not the subject but it return me a list of table names finishing by "_1")
Now what I would like to do is to display the content of all these tables in one command (just like if I was using cat *) and I would like to time this command.
So what should be the command ?
Thank you for your help.
This is not possible with a single SQL command.
You have to generate a series of SELECT statements, one for each table, and execute all of them.
I am using TSQLT AssertResultSetsHaveSameMetaData to compare metadata between two tables.But the problem is that i cannot hardcode the table name since i am passing the table name as the parameter at the runtime.So is there any way to do that
You use tSQLt.AssertResultSetsHaveSameMetaData by passing two select statements like this:
exec tSQLt.AssertResultSetsHaveSameMetaData
'SELECT TOP 1 * FROM mySchema.ThisTable;'
, 'SELECT TOP 1 * FROM mySchema.ThatTable;';
So it should be quite easy to parameterise the names of the tables you are comparing and build the SELECT statements based on those table name parameters.
However, if you are using the latest version of tSQLt you can also now use tSQLt.AssertEqualsTableSchema to do the same thing. You would use this assertion like this:
exec tSQLt.AssertEqualsTableSchema
'mySchema.ThisTable'
, 'mySchema.ThatTable';
Once again, parameterising the tables names would be easy since they are passed to AssertEqualsTableSchema as parameters.
If you explain the use case/context and provide sample code to explain what you are trying to do you stand a better chance of getting the help you need.
CREATE TABLE texhisowntable (age INTEGER, name (32));
in this emty TABLE i write infomation. First age, Second name up to 32, (string, numbers, ore chars i don't know witch one you would try, but i write Words )
..so lets do it:
INSERT INTO texhisowntable (age, name) VALUES(100, ''TheJavaRockS>|<RwithTheGoldenAxE");
so now im TheJavaRockS>|
SELECT * FROM texhisowntable;
and my command would say:"ACDC, let it Play, you are old but you look fine"
//he would print : TheJavaRockS>|
but im old and i forget things, so who knows how i can see, how i created the table.
I only want rom the commandprommt he print's
CREATE TABLE texhisowntable (age INTEGER, name (32));
13 ago, i answer my own Question. With the command ".schema" it shows every table for example in the file "thnkhsu.db" where also the TABLE "texhisowntable" is.
If you got much TABLES, and search only, the one you like, for example the "texhisowntable" in the file "thnkhsu.db" you only need to write
".schemas texhisowntable" and the command shows, how i created the TABLE.
;) its very simple
I have a SQLITE3 database wherein I have stored various columns. One column (cmd) in particular contains the full command line and associated parameters. Is there a way to extract just the first word in this column (just before the first space)? I am not interested in seeing the various parameters used, but do want to see the command issued.
Here's an example:
select cmd from log2 limit 3;
user-sync //depot/PATH/interface.h
user-info
user-changes -s submitted //depot/PATH/build/...#2011/12/06:18:31:10,#2012/01/18:00:05:55
From the result above, I'd like to use an inline SQL function (if available in SQLITE3) to parse on the first instance of space, and perhaps use a left function call (I know this is not available in SQLITE3) to return just the "user-sync" string. Same for "user-info" and "user-changes".
Any ideas?
Thanks.
My soluion:
sqlite> CREATE TABLE command (cmd TEXT);
sqlite> INSERT INTO command (cmd) VALUES ('ls'),('cd ~'),(' mpv movie.mkv ');
sqlite> SELECT substr(trim(cmd),1,instr(trim(cmd)||' ',' ')-1) FROM command;
ls
cd
mpv
Pros:
it's not that a dirty hack
it only uses core functions
"Finds the first occurrence" function is one of the SQLite3 Core Functions (http://www.sqlite.org/lang_corefunc.html).
Of course, it is much better to use instr(X,Y).
So you can write:
SELECT substr(cmd,1,instr(cmd,' ')-1) FROM log2
As the position of your first space character is unknown, I don't think there is a corefunction in SQLite that will help.
I think you'll have to create one http://www.sqlite.org/c3ref/create_function.html
Here's a hack
sqlite> create table test (a);
sqlite> insert into test values ("This is a test.");
sqlite> select * from test;
This is a test.
sqlite> select rtrim(substr(replace(a,' ','----------------------------------------------------------------------------------------'),1,80),'-') from test;
This
It works as long as your longest command is less than 80 characters (and you include 80 '-' characters in the substitution string -- I didn't count them!). If your commands can contain '-' just use a different character that is not allowed in the commands.
I don't believe that's something you'll be able to do within the SQL itself. SQLite's support for string handling functions is not as extensive as other RDBMSs (some of which would let you do a SUBSTR with a Reg Exp).
My suggestion is either to write your own SQL function as suggested by #Jon or just do it as a post-processing step in your app code.